- Start Date: 2014-07-02
- RFC PR: rust-lang/rfcs#151
- Rust Issue: rust-lang/rust#12831
Summary
Closures should capture their upvars by value unless the ref
keyword is used.
Motivation
For unboxed closures, we will need to syntactically distinguish between captures by value and captures by reference.
Detailed design設計(する)
This is a small part of #114, split off to separate it from the rest of the discussion going on in that RFC.
Closures should capture their upvars (closed-over variables) by value unless the ref
keyword precedes|
of the argument|x| x + 2
will capture x
by value (and thus,x
is not Copy
, it will move x
into the closure), but ref |x| x + 2
will capture x
by reference.
In an unboxed-closures world, the immutability/mutability of the borrow (as the case may be) is inferred from the type of the closure: Fn
captures by immutableFnMut
captures by mutable reference.
Drawbacks
It may be that ref
is unwanted complexity; it only changes the semantics of 10%-20% of closures, after all. This does not add any core functionality to the language,
Alternatives代わりのもの、選択肢
As above, the impact of not doing this is that reference
Another alternative
Unresolved questions
None.