std-method

BTreeSet::range

Select an interval of members. The returned iterator borrows the set and respects included and excluded endpoints. Reversed bounds or equal excluded endpoints can panic. Expected stdout (not executed): [6, 8] Common mistake: Assuming Excluded includes its boundary.

Formal Syntax & Structure

rust syntaxrust
fn range<K, R>(&self, range: R) -> Range<'_, T> where K: Ord + ?Sized, T: Borrow<K> + Ord, R: RangeBounds<K>

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeSet;    use std::ops::Bound::{Excluded, Included};    let seats = BTreeSet::from([2, 4, 6, 8, 10]);    let picked: Vec<_> = seats.range((Excluded(4), Included(8))).copied().collect();    println!("{picked:?}");}
Rust Reference