-
Open the Cargo.toml file that has been generated earlier for you.
-
In the bin folder, create a file called iterator_step_by.rs.
-
Add the following code, and run it with cargo run --bin iterator_step_by:
1 #![feature(iterator_step_by)] 2 3 fn main() { 4 // step_by() will start on the first element of an iterator, 5 // but then skips a certain number of elements on every
iteration 6 let even_numbers: Vec<_> = (0..100).step_by(2).collect(); 7 println!("The first one hundred even numbers: {:?}",
even_numbers); 8 9 // step_by() will always start at the beginning. 10 // If you need to skip the first few elements as well, use
skip() 11 let some_data = ["Andrei", "Romania", "Giuseppe", "Italy",
"Susan", "Britain"]; 12 let countries: Vec<_> =
some_data.iter().skip(1).step_by(2).collect(); 13 println!("Countries in the data: {:?}", countries); 14 15 let grouped_stream = "Aaron 182cm 70kg Alice 160cm 90kg Bob
197cm 83kg"; 16 let weights: Vec<_> = grouped_stream 17 .split_whitespace() 18 .skip(2) 19 .step_by(3) 20 .collect(); 21 println!("The weights of the people are: {:?}", weights); 22 }