Reputation: 8533
I want to sort then dedup a vector of i32
in rust:
fn main() {
let mut vec = vec![5,5,3,3,4,4,2,1,2,1];
vec.sort().collect().dedup();
println!("{:?}", vec);
}
This code does not work, but if the dedup
part is done this way, it's fine:
vec.sort();
vec.dedup();
How do I do sort
and dedup
in one line in my example ?
Upvotes: 1
Views: 597
Reputation: 58715
With itertools
, you can do:
use itertools::Itertools;
fn main() {
let mut vec = vec![5,5,3,3,4,4,2,1,2,1];
vec = vec.into_iter().sorted().dedup().collect();
println!("{:?}", vec);
}
It really depeonds on why you think you need a one-liner though. If it's actually because you need a single expression then you can use a block:
vec = {
vec.sort();
vec.dedup();
vec
};
Upvotes: 7