-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
# closure | ||
|
||
# 闭包 Closure | ||
|
||
可以捕获外部变量, 变量有三种方法被捕获: | ||
- 只读引用 (read only) | ||
- 可变引用 (mutable, modified) | ||
- 值 (consumed), 如果该值实现了 `Copy` trait, 那就复制一份; 如果未实现, 就将该值 `move` 到 closure中. | ||
|
||
当然, 也可以显式地加一个 `move` 标记, 将一个变量移到closure内. | ||
|
||
自加1: | ||
```rust | ||
let accum = |s: i32| -> i32 { s + 1 }; | ||
``` | ||
|
||
返回一个常数1: | ||
```rust | ||
let one = || 1; | ||
``` | ||
|
||
也可以获取被捕获变量的所有权: | ||
```rust | ||
let square = move |point: Point| -> i32 { point.x + point.y }; | ||
``` | ||
|
||
Rust中的闭包性能跟一般的函数一样, 而且要比函数指针还要快. | ||
|
||
## 作为输出参数 | ||
一个高阶函数也可以返回一个函数, 但需要加入 `impl` 前缀. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
|
||
# Fn, FnMut and FnOnce | ||
|
||
作为输入参数. 比如, 作为一个高阶函数的输入参数时, 闭包 closure 可以有三种声明: | ||
- Fn, 对应于 "引用" (reference) | ||
- FnMut, 对应于 "可变引用" (mutable reference) | ||
- FnOnce, 对应于 "值" (consumed value) | ||
|
||
## Generic | ||
Function Trait: | ||
```rust | ||
fn apply_to_3<F>(f: F) -> i32 | ||
where F: Fn(i32) -> i32, | ||
{ | ||
return f(3); | ||
} | ||
``` | ||
|
||
区别: | ||
- `fn(...) -> ...`, 函数类型,只能是一般的函数 | ||
- `Fn(...) -> ...`, 泛型函数,可以是一般的函数, 也可以是闭包 | ||
|
||
## FnOnce | ||
`FnOnce()` 这种声明的函数, 只能被调用一次, 通常是因为有值移到了函数内部, 转移了所有权. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,2 @@ | ||
|
||
# 闭包 Closure | ||
|
||
可以捕获外部变量, 变量有三种方法被捕获: | ||
* 引用 (read only) | ||
* 可变引用 (mutable, modified) | ||
* 值 (consumed), 如果该值实现了 `Copy` trait, 那就复制一份; 如果未实现, 就将该值 `move` 到 closure中. | ||
|
||
当然, 也可以显式地加一个 `move` 标记, 将一个变量移到closure内. | ||
|
||
自加1: | ||
```rust | ||
let accum = |s: i32| -> i32 { s + 1 }; | ||
``` | ||
|
||
返回一个常数1: | ||
```rust | ||
let one = || 1; | ||
``` | ||
|
||
也可以获取被捕获变量的所有权: | ||
```rust | ||
let square = move |point: Point| -> i32 { point.x + point.y }; | ||
``` | ||
|
||
Rust中,闭包性能跟一般的函数一样,而且要比函数指针还要快。 | ||
|
||
## 作为输入参数 | ||
比如, 作为一个高阶函数的输入参数时, closure 可以有三种声明: | ||
* Fn, 对应于 `引用` | ||
* FnMut, 对应于 `可变引用` | ||
* FnOnce, 对应于 `值` | ||
|
||
## 作为输出参数 | ||
一个高阶函数也可以返回一个函数, 但需要加入 `impl` 前缀. | ||
# 函数 |