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

Chapter 4.2: code example does not compile. #2034

Closed
aiifabbf opened this issue Jul 19, 2019 · 2 comments
Closed

Chapter 4.2: code example does not compile. #2034

aiifabbf opened this issue Jul 19, 2019 · 2 comments

Comments

@aiifabbf
Copy link

Hi. I was reading Chaper 4 Section 2: Referencs and Borrowing and just noticed that this code example

let mut s = String::from("hello");

let r1 = &s; // no problem
let r2 = &s; // no problem
println!("{} and {}", r1, r2);
// r1 and r2 are no longer used after this point

let r3 = &mut s; // no problem
println!("{}", r3);

does not compile. But the description says it should compile

Note that a reference's scope starts from where it is introduced and continues through the last time that reference is used. For instance, this code will compile because the last usage of the immutable references occurs before the mutable reference is introduced:

[code example above]

The scopes of the immutable references r1 and r2 end after the println! where they are last used, which is before the mutable reference r3 is created. These scopes don't overlap, so this code is allowed.

This code piece is located right above Dangling References section.

My error message

benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ cat a.rs
pub fn main() {
    let mut s = String::from("hello");

    let r1 = &s; // no problem
    let r2 = &s; // no problem
    println!("{} and {}", r1, r2);
    // r1 and r2 are no longer used after this point

    let r3 = &mut s; // no problem
    println!("{}", r3);
}
benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ rustc a.rs
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
  --> a.rs:9:19
   |
4  |     let r1 = &s; // no problem
   |               - immutable borrow occurs here
...
9  |     let r3 = &mut s; // no problem
   |                   ^ mutable borrow occurs here
10 |     println!("{}", r3);
11 | }
   | - immutable borrow ends here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0502`.
benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ 

rustc version

benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ rustc --version
rustc 1.32.0 (9fda7c223 2019-01-16)

I guess there should be a block around where r1, r2 is defined? When I changed it to this

let mut s = String::from("hello");

{
    let r1 = &s; // no problem
    let r2 = &s; // no problem
    println!("{} and {}", r1, r2);
    // r1 and r2 are no longer used after this point
}

let r3 = &mut s; // no problem
println!("{}", r3);

it compiles without any problem.

@rusty-snake
Copy link

a.rs:

fn main() {
    let mut s = String::from("hello");

    let r1 = &s; // no problem
    let r2 = &s; // no problem
    println!("{} and {}", r1, r2);
    // r1 and r2 are no longer used after this point

    let r3 = &mut s; // no problem
    println!("{}", r3);
}
$ rustc --version --verbose
rustc 1.36.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.36.0
LLVM version: 8.0
$ rustc --edition=2018 -O -o a a.rs
$ ./a
hello and hello
hello

@carols10cents
Copy link
Member

Hi @aiifabbf, this example compiles with the Non-Lexical Lifetimes (NLL) feature. With Rust 1.32, you'll need to opt in to the 2018 edition, either by passing the --edition=2018 flag as @rusty-snake demonstrated, or you can build your project using cargo instead of using rustc as discussed in Chapter 1 and put edition = "2018" in your Cargo.toml.

The book is written using the 2018 edition, which we talk about on the front page of the book.

If you upgrade to Rust 1.36, NLL is enabled in the default 2015 edition too, so this example will compile as shown in this playground that has its edition set to 2015 and currently uses stable Rust 1.36.

Please let me know if you have any further questions about this. Thanks!

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