-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Description
Hello, I apologize if this is the incorrect place for book feedback. When looking at the main page and its links, I did not find any other instructions for submitting feedback https://doc.rust-lang.org/book/title-page.html
In Chapter 4 Section 2 https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html about half way down the page is a section that begins with
"As always, we can use curly brackets to create a new scope, allowing for multiple mutable references, just not simultaneous ones:"
let mut s = String::from("hello");
{
let r1 = &mut s;
} // r1 goes out of scope here, so we can make a new reference with no problems.
let r2 = &mut s;I do not understand this suggestion to wrap the first mutable reference in curly braces. I'm attempting to run through some exercises, but I'm having trouble filling in the gaps in this code sample.
Later on down the page it says:
"Note that a reference’s scope starts from where it is introduced and continues through the last time that reference is used."
So if we create a reference and then use it before we create a second reference, the code works fine.
let mut s = String::from("hello");
let r1 = &mut s;
println!("{}", r1);
let r2 = &mut s;
println!("{}", r2);Could you please help me understand how the curly braces from listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes https://github.com/rust-lang/book/blob/2bd5d42c9956369132228da6409f0e68da56c51a/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs help with multiple mutable references?
If there is a better place to have this discussion, I apologize and I would be happy to move the topic elsewhere. I just figured that you may appreciate some feedback from a newcomer and a fresh set of eyes. I personally found the curly braces code example to be a bit confusing, and I could use some help understanding it. Maybe it would also help future readers if we could clarify the section in the book about curly braces and mutable references, thanks! 😄