diff --git a/Examples.md b/Examples.md index 6c2cad3..864a004 100644 --- a/Examples.md +++ b/Examples.md @@ -866,7 +866,7 @@ fn main ``` -#### loop +#### while @@ -908,3 +908,83 @@ fn main ```
Rust
+ +#### for loops +This subsection introduces the three ways of converting collections into iterators. For convenience, we combine them into a single example here. Note that Husht will provide three different prepositions for looping as syntactic sugar for the three methods of obtaining an iterator from the collection. The proposal below uses `in` as Rust does, `from` for borrowing, and `on` for borrowing mutably. We should also consider `in`, `&in`, and `&mut in`, as that might be more Rust-y. + + + + + + + +
RustHusht
+ +``` +fn main() { + let mut names = vec!["Bob", "Frank", "Ferris"]; + + for name in names.iter() { + match name { + &"Ferris" => println!("There is a rustacean among us!"), + _ => println!("Hello {}", name), + } + } + + println!("names: {:?}", names); + + for name in names.iter_mut() { + *name = match name { + &mut "Ferris" => "There is a rustacean among us!", + _ => "Hello", + } + } + + println!("names: {:?}", names); + + for name in names.into_iter() { + match name { + "Hello" => println!("Someone was normal"), + _ => println!("Warning: {}", name), + } + } + // names has moved here and is unusable: + // println!("names: {:?}", names); // compile error +} +``` + + +``` +fn main + let mut names = vec!["Bob", "Frank", "Ferris"] + + for name from names + match name + &"Ferris" => println! "There is a rustacean among us!" + _ => println! "Hello {}", name + + println!("names: {:?}", names); + + + + for name on names + *name = match name + &mut "Ferris" => "There is a rustacean among us!" + _ => "Hello" + + println!("names: {:?}", names); + + + + for name in names + match name + "Hello" => println! "Someone was normal" + _ => println! "Warning: {}", name + + // names has moved here and is unusable: + // println!("names: {:?}", names); // compile error + + + +``` +
\ No newline at end of file