std-method

Iterator::take

Limit the output to at most n items. Useful for bounding infinite sources and previewing results. Expected stdout (not executed): [100, 101, 102] Common mistake: Assuming take pads a short sequence to n elements.

Formal Syntax & Structure

rust syntaxrust
fn take(self, n: usize) -> Take<Self> where Self: Sized

Working Example

Example Snippetrust
fn main() {    let preview: Vec<_> = (100..).take(3).collect();    println!("{preview:?}");}
Rust Reference