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

Bad example in "Borrowed Pointers"? #96

Open
ThirteenPeeps opened this issue May 29, 2020 · 1 comment
Open

Bad example in "Borrowed Pointers"? #96

ThirteenPeeps opened this issue May 29, 2020 · 1 comment

Comments

@ThirteenPeeps
Copy link

The example given is:

fn foo() {
    let mut x = 5;            // type: i32
    {
        let y = &x;           // type: &i32
        //x = 4;              // Error - x has been borrowed
        println!("{}", x);    // Ok - x can be read
    }
    x = 4;                    // OK - y no longer exists
}

But when I tested this, uncommenting the x = 4 line, it built and ran just fine. The same was true for the next example where y = &mut x: when I uncommented the two releavant lines it still ran.

I think either this is a bad example of the concept you were trying to communicate (borrowed values becoming temporarily immutable), or the concept has been changed over time and the material should be updated.

HOWEVER I am just learning Rust, so I may be utterly mistaken :)

@nrc
Copy link
Owner

nrc commented Jun 3, 2020

Huh, presumably due to non-lexical lifetimes. I think changing println!("{}", x); to println!("{}", y); should fix this, although maybe we should add rather than replace so that the comment on that line still makes sense

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

2 participants