std-method

slice::sort

Sort elements in ascending Ord order in place. This is stable: values that compare equal retain their relative order. The implementation may allocate temporary memory. Expected stdout (not executed): [2, 2, 5, 8] Common mistake: Trying to assign the return value to a new sorted array; sort returns unit.

Formal Syntax & Structure

rust syntaxrust
slice.sort()

Working Example

Example Snippetrust
fn main() {    let mut values = [8, 2, 5, 2];    values.sort();    println!("{values:?}");}
Rust Reference