From 7f8bfc07d180adc1ab9150dc55417c92e6546aa1 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 25 Aug 2024 17:51:07 +0000 Subject: [PATCH] Update Examples --- Examples.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) 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