std-method

slice::partition_point

Find the boundary where a predicate switches from true to false in a partitioned slice. Two such boundaries locate an entire run of equal keys in sorted data. Expected stdout (not executed): 1..4: [2, 2, 2] Common mistake: Using a predicate that alternates between true and false across the slice.

Formal Syntax & Structure

rust syntaxrust
slice.partition_point(|item| predicate)

Working Example

Example Snippetrust
fn main() {    let data = [1, 2, 2, 2, 4];    let start = data.partition_point(|n| *n < 2);    let end = data.partition_point(|n| *n <= 2);    println!("{start}..{end}: {:?}", &data[start..end]);}
Rust Reference