closure example should panic?
ensconced opened this issue · 2 comments
ensconced commented
I may be missing something, but the text says that this snippet from the closures section "Can't be done", but it seems to compile without any trouble (using rustc 1.38.0). Thanks for your work on this guide!
.
fn main() {
let mut s = "world";
// closure does a mutable borrow of s
let mut changer = || s = "world";
changer();
// does an immutable borrow of s
assert_eq!(s, "world");
}
stevedonovan commented
It's definitely time for a new edition! NLL makes a lot of lifetime issues go away...
whoan commented
I ended up here from your post Why Rust Closures are (Somewhat) Hard (very useful, BTW). The (similar) example in your post also compiles now:
let mut x = 1.0;
let mut change_x = || x = 2.0;
change_x();
println!("{}",x);
Thanks for sharing!