Skip to content
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

closure example should panic? #92

Open
ensconced opened this issue Oct 3, 2019 · 2 comments
Open

closure example should panic? #92

ensconced opened this issue Oct 3, 2019 · 2 comments

Comments

@ensconced
Copy link

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
Copy link
Owner

It's definitely time for a new edition! NLL makes a lot of lifetime issues go away...

@whoan
Copy link

whoan commented Apr 18, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants