Add [Closure] chapter

This commit is contained in:
sunface
2022-03-31 13:49:17 +08:00
parent 8220143688
commit 562eb9247f
4 changed files with 91 additions and 6 deletions

View File

@ -231,4 +231,55 @@ fn main() {
call_me(closure);
call_me(function);
}
```
10、
```rust
/* Fill in the blank and fix the errror */
// You can aslo use `impl FnOnce(i32) -> i32`
fn create_fn() -> impl Fn(i32) -> i32 {
let num = 5;
move |x| x + num
}
fn main() {
let fn_plain = create_fn();
fn_plain(1);
}
```
```rust
/* Fill in the blank and fix the errror */
fn create_fn() -> Box<dyn Fn(i32) -> i32> {
let num = 5;
// how does the following closure capture the evironment variable `num`
// &T, &mut T, T ?
Box::new(move |x| x + num)
}
fn main() {
let fn_plain = create_fn();
fn_plain(1);
}
```
11、
```rust
// Every closure has its own type. Even if one closure has the same representation as another, their types are different.
fn factory(x:i32) -> Box<dyn Fn(i32) -> i32> {
let num = 5;
if x > 1{
Box::new(move |x| x + num)
} else {
Box::new(move |x| x + num)
}
}
fn main() {}
```