Iterators

Another important functional aspect is that of lazy iteration. Given a collection of types, one should be able to loop over those or a subset in any given order. In Rust, a common iterator is a range which has a start and an end. Let's look at how these work:

// chapter2/range.rs#![feature(inclusive_range_syntax)]fn main() {    let numbers = 1..5;    for number in numbers {        println!("{}", number);    }    println!("------------------");    let inclusive = 1..=5;    for number in inclusive {        println!("{}", number);    }}

The first range is an exclusive range that spans from the first element to the last but one. The second range is an inclusive one which spans till the last element. Note that inclusive range is an experimental feature that might change ...

Get Network Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.