-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chapter 13 about FnOnce clousure definition #4011
Comments
Like this : let x = String::from("hello");
let consume_x = move || {
println!("{}", x);
};
consume_x(); // closure take the ownership
// consume_x(); // can not execute because `x` is moved |
This is a tricky one, but the terminology here is correct. If you try running the code you showed, it works just fine! Here’s a playground which demonstrates that. (Also notice that If you returned the value from the closure at the end, like this, however, it would only implement let x = String::from("hello");
let consume_x = move || {
println!("{}", x);
x
};
consume_x(); // closure take the ownership
consume_x(); // can not execute because `x` is moved This has the error you expected from the There are two main ways to move a value out of a closure: returning it, or passing it to a function which takes ownership of it. Hope that helps! |
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch13-01-closures.html#moving-captured-values-out-of-closures-and-the-fn-traits
I thought it could be: A closure that moves captured values in of its body will only implement
FnOnce
?in or out?
The text was updated successfully, but these errors were encountered: