std-method

Iterator::is_sorted — check sequence order

Check nondecreasing order without rearranging elements. An empty or single-item sequence passes; incomparable adjacent values fail. Stable since 1.82.0. Expected stdout (not executed): true false Common mistake: Assuming is_sorted sorts or changes a collection.

Formal Syntax & Structure

rust syntaxrust
fn is_sorted(self) -> bool where Self: Sized, Self::Item: PartialOrd

Working Example

Example Snippetrust
fn main() {    println!("{}", [1, 2, 2, 6].into_iter().is_sorted());    println!("{}", [2, 1, 6].into_iter().is_sorted());}
Rust Reference