How to sort a vector in Rust: complete guide with examples
Hello everyone! I just started learning Rust and encountered a problem with sorting a vector. In other languages like Python or JavaScript, it's done in one line, but here I'm completely confused.
I have a vector of numbers:
```rust
let mut numbers = vec![5, 2, 8, 1, 9, 3];
```
I want to sort it in ascending order. I tried doing this:
```rust
numbers.sort();
```
But the compiler gives an error: "the trait `std::cmp::Ord` is not implemented for `i32`". Although I thought for numbers it should work out of the box.
I also have a vector of strings:
```rust
let mut fruits = vec!["banana", "apple", "cherry", "date"];
```
Here sorting doesn't work at all — the compiler writes about borrowing and moving.
Also I tried sorting structures:
```rust
struct Person {
nam
No answers yet.
Log in to leave an answer