Update Examples
parent
880ff999ff
commit
ca50e00531
56
Examples.md
56
Examples.md
@ -1079,4 +1079,58 @@ fn main
|
||||
let another = x: i64 -> i64 --> x*x
|
||||
// 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.
|
||||
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