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

先行する
the opening | of the argument
引数
list.
リスト、列挙する
Thus
それゆえに、従って、
|x| x + 2 will capture x by value (and thus,
それゆえに、従って、
if 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 immutable

不変の
reference,
参照
while FnMut captures by mutable reference.
参照
In a boxed-closures world, the borrows are always mutable.

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,

言語
as a reference
参照
can always be made explicitly
明示的に
and then captured. However, there are a lot of closures, and the workaround to capture a reference
参照
by value is painful.

Alternatives
代わりのもの、選択肢

As above, the impact of not doing this is that reference

参照
semantics would have to be achieved. However, the diff against current Rust was thousands of lines of pretty ugly code.

Another alternative

代わりのもの、選択肢
would be to annotate each individual
個々の、それぞれの
upvar with its capture semantics, like capture clauses
句、節
in C++11. This proposal does not preclude adding
たす
that functionality should it be deemed useful in the future. Note that C++11 provides
与える
a syntax
文法
for capturing all upvars by reference,
参照
exactly
正確に
as this proposal does.

Unresolved questions

None.