std-type

Reverse<T> — reverse a sort key

Reverse wraps a value and reverses its comparison order when the inner type supports ordering. It does not reverse a collection by itself. Use it as a key to sort higher priorities first. Expected stdout (not executed): [9, 4, 2] Common mistake: Calling Reverse on a list and expecting the list elements to be reordered.

Formal Syntax & Structure

rust syntaxrust
pub struct Reverse<T>(pub T);

Working Example

Example Snippetrust
fn main() {    let mut scores = [2, 9, 4];    scores.sort_by_key(|&n| std::cmp::Reverse(n));    println!("{scores:?}");}
Rust Reference