You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey! I'm a beginner, but something worked and on the guide it said it shouldn't have worked.
On the "Borrowing Pointers" section, we've been given this code section:
fnfoo(){letmut x = 5;// type: i32{let y = &mut x;// type: &mut i32//x = 4; // Error - x has been borrowed//println!("{}", x); // Error - requires borrowing x}
x = 4;// OK - y no longer exists}
First, on lines x = 4 and println!... the comment is a direct contradiction.
Second, uncommenting the println! does work. I'm not sure what that means though, but
something here is unclear.
EDIT: This is also true when uncommenting x = 4. I'm not really sure about the validity of this example.
Thank you!
The text was updated successfully, but these errors were encountered:
I think this is a result of changing from lexical to non-lexical lifetimes, previously lifetimes would always continue to the end of explicit scopes (mostly that means to a }), now however, lifetimes are terminated as soon as they can be. We could fix the example (i.e., cause the commented lines to be errors again) by adding a use of y after the commented lines and before the } on it's own line, e.g.:
fn foo() {
let mut x = 5; // type: i32
{
let y = &mut x; // type: &mut i32
//x = 4; // Error - x has been borrowed
//println!("{}", x); // Error - requires borrowing x
println!("{}", y);
}
x = 4; // OK - y no longer exists
}
Hey! I'm a beginner, but something worked and on the guide it said it shouldn't have worked.
On the "Borrowing Pointers" section, we've been given this code section:
First, on lines
x = 4
andprintln!...
the comment is a direct contradiction.Second, uncommenting the
println!
does work. I'm not sure what that means though, butsomething here is unclear.
EDIT: This is also true when uncommenting
x = 4
. I'm not really sure about the validity of this example.Thank you!
The text was updated successfully, but these errors were encountered: