Update Examples

Glen Whitney 2024-08-25 17:51:07 +00:00
parent a9db9e14c0
commit 7f8bfc07d1

@ -866,7 +866,7 @@ fn main
```
</td></tr></table>
#### loop
#### while
<table>
<tr>
<th>Rust</th>
@ -906,5 +906,85 @@ fn main
n += 1;
```
</td></tr></table>
#### 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.
<table>
<tr>
<th>Rust</th>
<th>Husht</th>
</tr>
<tr>
<td>
```
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
}
```
</td><td>
```
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
```
</td></tr></table>