std-method

BTreeMap::split_off

Partition records at a key. The original retains keys strictly below the boundary; the returned map owns keys at or above it. Expected stdout (not executed): [1] [4, 7] Common mistake: Expecting the boundary to remain in the original.

Formal Syntax & Structure

rust syntaxrust
fn split_off<Q>(&mut self, key: &Q) -> BTreeMap<K, V> where K: Borrow<Q> + Ord, Q: Ord + ?Sized

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeMap;    let mut weeks = BTreeMap::from([(1, "a"), (4, "b"), (7, "c")]);    let later = weeks.split_off(&4);    println!("{:?} {:?}", weeks.keys().collect::<Vec<_>>(), later.keys().collect::<Vec<_>>());}
Rust Reference