2b4b8eda-7169-4b6d-80a0-be0958917703
prompter
Could you please write me Rust code that takes ownership of an array of integers and sorts them in ascending order?
cbb7237d-49f9-470c-ba1d-7655ace5b6bd
2b4b8eda-7169-4b6d-80a0-be0958917703
assistant
Sure, here's an implementation in Rust:

```
fn sort_ascending(arr: &mut [i32]) {
    arr.sort_unstable();
}

fn main() {
    let mut arr = [5, 2, 4, 1, 3];
    sort_ascending(&mut arr);
    println!("{:?}", arr);
}
```

This code sorts the array arr in place using the sort_unstable method. The sort_unstable method sorts the slice in place and may not preserve the order of equal elements.
4d873d4c-1d30-4d16-9a30-c7adaff83922
cbb7237d-49f9-470c-ba1d-7655ace5b6bd
prompter
do it in descending order
e2759a4f-d719-4c62-9e7f-ef12dc0ac96f
4d873d4c-1d30-4d16-9a30-c7adaff83922
assistant
Sure! To sort an array of integers in descending order, you can create a sort_descending function like this:

```
fn sort_descending(arr: &mut [i32]) {
    arr.sort_unstable_by(|a, b| b.cmp(a));
}

fn main() {
    let mut arr = [5, 2, 4, 1, 3];
    sort_descending(&mut arr);
    println!("{:?}", arr);
}
```

Here, the `sort_unstable_by` method is used with a comparison closure that reverses the ordering of elements. The `cmp` method returns an ordering value that represents the relative order of two elements. Reversing the order of the arguments to `cmp` will cause it to return a value that represents the opposite ordering, which is what we need for a descending sort.