A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-35.rs
1fnmain(){
2letmutv=vec![3,1,2,5,4];
3v.sort_by(|a,b|b.cmp(a));
4println!("{:?}",v);
5}
Closures
Answer & explanation
Console output
[5, 4, 3, 2, 1]
Why
`sort_by` takes a comparator closure returning an `Ordering`. Calling `b.cmp(a)` flips the usual `a.cmp(b)`, so the vector sorts in descending order: `[5, 4, 3, 2, 1]`. Swapping the comparison operands is the idiomatic way to reverse a sort.