Lööps
https://doc.rust-lang.org/reference/expressions/loop-expr.html
Anything that is iterable can be used in a for in loop. We've seen a range being used, but it also works with a Vec:
fn main() { for i in vec![52, 49, 21] { println!("I like the number {}", i); } }
Or a slice:
fn main() { for i in &[52, 49, 21] { println!("I like the number {}", i); } } // output: // I like the number 52 // I like the number 49 // I like the number 21
Or an actual iterator:
fn main() { // note: `&str` also has a `.bytes()` iterator. // Rust's `char` type is a "Unicode scalar value" for c in "rust".chars() { println!("Give me a {}", c); } } // output: // Give me a r // Give me a u // Give me a s // Give me a t
Even if the iterator items are filtered and mapped and flattened:
fn main() { for c in "SuRPRISE INbOUND" .chars() .filter(|c| c.is_lowercase()) .flat_map(|c| c.to_uppercase()) { print!("{}", c); } println!(); } // output: UB