Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Count number of elements of an iterator

  • count
fn main() {
    let numbers = [3, 4, 5];
    println!("{}", numbers.len());
    println!("{}", numbers.iter().count());

    // An iterator
    let readdir = std::path::Path::new(".").read_dir().unwrap();
    println!("{:?}", readdir);
    println!("{}", readdir.count());
}
3
3
ReadDir(".")
5