std-method

Iterator::partition — split into two destinations

Consume the sequence and extend two collections, one for accepted items and one for rejected items. Choose the destination types explicitly when inference cannot decide. Expected stdout (not executed): [4, 2] [0, 0] Common mistake: Expecting a borrowed partition of the original collection.

Formal Syntax & Structure

rust syntaxrust
fn partition<B, F>(self, f: F) -> (B, B) where Self: Sized, B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool

Working Example

Example Snippetrust
fn main() {    let (ready, waiting): (Vec<_>, Vec<_>) = [4, 0, 2, 0].into_iter().partition(|n| *n > 0);    println!("{ready:?} {waiting:?}");}
Rust Reference