Codula
Codula

Reputation: 41

How to sort a part of a vector?

In C++ We can easily sort a part of an vector By

sort(A.begin()+start,A.begin()+end);

But in Rust I can not find a good method,Can Anyone Help me??

Upvotes: 4

Views: 748

Answers (1)

lkolbly
lkolbly

Reputation: 1240

You can call sort() on a mutable slice, such as a section of your vector like this:

fn main() {
    let mut v: Vec<u32> = vec![7, 6, 5, 4, 3, 2, 1];
    v[2..5].sort();
    println!("{:?}", v);
}

(Playground)

Upvotes: 12

Related Questions