Update Examples
parent
880ff999ff
commit
ca50e00531
54
Examples.md
54
Examples.md
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user