Update Examples

Glen Whitney 2024-08-27 01:59:14 +00:00
parent 880ff999ff
commit ca50e00531

@ -1080,3 +1080,57 @@ fn main
// but not too bad I think
```
Of course, there's no real reason to implement that until/unless we actually want anonymous regular (non-closure) functions.
##### Capturing
<table>
<tr>
<th>Rust</th>
<th>Husht</th>
</tr>
<tr>
<td>
```
fn main() {
use std::mem;
let consume = || {
println!("`movable`: {:?}", movable);
mem::drop(movable);
};
// `consume` consumes the variable so this can only be called once.
consume();
let haystack = vec![1, 2, 3];
let contains = move |needle| haystack.contains(needle);
println!("{}", contains(&1));
println!("{}", contains(&4));
}
```
</td><td>
```
fn main
use std::mem
let consume = =>
println! "`movable`: {:?}", movable
mem::drop movable
// `consume` consumes the variable so this can only be called once.
consume()
let haystack = vec![1, 2, 3]
let contains = move needle => haystack.contains needle
println! "{}", contains(&1) //! That we need the & is Rust weirdoness
println! "{}", contains(&4)
```
</td></tr></table>