- 捕获
- 参见:
- 参见:
捕获
闭包本身是相当灵活的,可以实现所需功能来让闭包运行而不用类型标注(原文:Closures are inherently flexible and will do what the functionality requires
to make the closure work without annotation)。这允许变量捕获灵活地适应使用
情况,有时是移动(moving)有时是借用(borrowing)(原文:This allows capturing to
flexibly adapt to the use case, sometimes moving and sometimes borrowing.)。闭包可以捕获变量:
- 通过引用:
&T - 通过可变引用:
&mut T - 通过值:
T
它们更倾向于通过引用来捕获变量并且只在需要时才用后面用法(原文:They preferentially capture variables by reference and only go lower when
required.)。
fn main() {use std::mem;let color = "green";// 闭包打印 `color`,它会马上借用(`&`)`color` 并将该借用和闭包存储// 到 `print` 变量中。它会一保持借用状态直到 `print` 离开作用域。// `println!` 只需要`通过引用`,所以它没有采用更多任何限制性的内容。// (原文:`println!` only requires `by reference` so it doesn't// impose anything more restrictive.)let print = || println!("`color`: {}", color);// 使用借用来调用闭包。print();print();let mut count = 0;// 闭包使 `count` 值增加,可以使用 `&mut count` 或者 `count`,// 但 `&mut count` 限制更少,所以采用它。立刻借用 `count`。// (原文: A closure to increment `count` could take either// `&mut count` or `count` but `&mut count` is less restrictive so// it takes that. Immediately borrows `count`.)//// `inc` 前面需要加上 `mut`,因为 `&mut` 会必存储的内部。// 因此,调用该闭包转变成需要一个 `mut` 的闭包。// (原文:A `mut` is required on `inc` because a `&mut` is stored// inside. Thus, calling the closure mutates the closure which requires// a `mut`.)let mut inc = || {count += 1;println!("`count`: {}", count);};// 调用闭包。inc();inc();//let reborrow = &mut count;// ^ 试一试: 将此行注释去掉。// 不可复制类型(non-copy type)。let movable = Box::new(3);// `mem::drop` requires `T` so this must take by value. A copy type// would copy into the closure leaving the original untouched.//// `mem::drop` 要求 `T`,所以这必须通过值来实现(原文:`mem::drop`// requires `T` so this must take by value.)。可复制类型将会复制// 值到闭包而不会用到原始值。不可复制类型必须移动(move),从而// `可移动`(movable) 立即移动到闭包中(原文:A non-copy must// move and so `movable` immediately moves into the closure)。let consume = || {println!("`movable`: {:?}", movable);mem::drop(movable);};// `consume` 消费(consume)了该变量,所以这只能调用一次。consume();//consume();// ^ 试一试:将此行注释去掉。}
参见:
Box 和 std:
:drop
