3 ms·
Thanks for the explanation, but how would the move keyword know that you're referring to the i variable? Ie - what is there were multiple variables (such as a f
by kentosi 11y ago
Thanks for the explanation, but how would the move keyword know that you're referring to the i variable? Ie - what is there were multiple variables (such as a for loop with j in there)?
Wouldn't it have been a better approach to add some sort of demarkation, such as i* or i^ (or whatever) to indicate this?
Just curious.
- Chilinot 11y agoIt moves everything you are referencing in the closure from the outer scope to the closure.
- Manishearth 11y agoA closure captures all variables from its environment that it is using (no more than that). They can be captured by reference (which is the default), or by-move (which is done with the `move` keyword). In case it is being captured by move, _all_ captured variables will be moved. If you wish to capture a specific variable by reference in a move closure, create a reference (`let y = &x`) outside of the closure and use the reference y instead of x inside.
- pmarreck 11y agoI agree that this seems ugly
- sixbrx 11y agoIn short, the "move everything" nature of "move" keyword here is not an issue because you can just make references for items you don't want moved. References themselves are "moved" by just copying their addresses.
- masklinn 11y ago> Wouldn't it have been a better approach to add some sort of demarkation, such as i* or i^ (or whatever) to indicate this? That's the path C++ took[0], the Rust people thought it had too much syntactic and semantic overhead, and that having just "move" and "referring" closures would be much simpler. If you want to mix them up, it's easy enough to create references outside the closure (and capture them by value with a move closure) [0] http://en.cppreference.com/w/cpp/language/lambda#Lambda_capture http://en.cppreference.com/w/cpp/language/lambda#Lambda_capt...
- steveklabnik 11y agoThere was a really good thread on this on /r/urust: https://www.reddit.com/r/rust/comments/46w4g4/what_is_rusts_lambda_syntax_and_design_rationale/ https://www.reddit.com/r/rust/comments/46w4g4/what_is_rusts_... In particular, don't miss this post: https://www.reddit.com/r/rust/comments/46w4g4/what_is_rusts_lambda_syntax_and_design_rationale/d08hee0 https://www.reddit.com/r/rust/comments/46w4g4/what_is_rusts_...