Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a part of a vector?

Tags:

sorting

rust

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??

like image 835
Codula Avatar asked Aug 31 '25 22:08

Codula


1 Answers

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)

like image 109
lkolbly Avatar answered Sep 04 '25 00:09

lkolbly