std-method
Iterator::by_ref
Borrow the iterator itself to run a consuming adapter on the borrowed receiver. The original iterator remains available at its advanced position when the borrow ends. Expected stdout (not executed): [1, 2] [3, 4, 5] Common mistake: Expecting by_ref to rewind or copy the cursor.
Formal Syntax & Structure
rust syntaxrust
fn by_ref(&mut self) -> &mut Self where Self: SizedWorking Example
Example Snippetrust
fn main() { let mut events = 1..6; let prefix: Vec<_> = events.by_ref().take(2).collect(); let rest: Vec<_> = events.collect(); println!("{prefix:?} {rest:?}");}