3 ms·
This was a fairly explicit design decision, because these cases come up so rarely. It's our opinion that it's not common enough to be worth increasing the compl
by steveklabnik 8y ago
This was a fairly explicit design decision, because these cases come up so rarely. It's our opinion that it's not common enough to be worth increasing the complexity of the language.
You don't even need it in this case; this will compile, for example:
fn main() {
let settings = String::new();
let channel = String::new();
let closure_with_hypothetical_syntax =
|| { some_code(&settings, channel) };
}
fn some_code(settings: &String, channel: String) {
}
The only difference is the & on settings in the closure. This moves channel but not settings.
- gpm 8y agoWait... hmm... now I'm going to have to track down some code where I did this trick and figure out why or if I was just being stupid.
- steveklabnik 8y agoIt’s cool! In general, the default is “rust tries to capture each thing the way it needs to be” and you can use move to override that. Sometimes, you do have to do what you wrote, at least in theory. I’ve never actually had to.
- chrismorgan 8y agoIf your closures need to be 'static, it’s common in my experience to need to use `move`, and thus to need to clone types like Rc<T>. It depends on whether you’re dealing with Fn, FnMut or FnOnce, too.