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

about the borrowing example #71

Open
trichimtrich opened this issue May 6, 2024 · 2 comments
Open

about the borrowing example #71

trichimtrich opened this issue May 6, 2024 · 2 comments
Labels

Comments

@trichimtrich
Copy link

Hi, I'm new to Rust and trying the sample in this doc
https://learning-rust.github.io/docs/borrowing/

The code

fn main() {
  let mut a = vec![1, 2, 3];
  let b = &mut a;  //  &mut borrow of `a` starts here
  // some code
b[1] = 123;

  println!("{:?}", a); // trying to access `a` as a shared borrow, so giving an error
}                  //  &mut borrow of `a` ends here

It seems to compile and run fine without any error on my machine

ubt :: testbin ‹master› % rustc --version                      1 ↵
rustc 1.78.0 (9b00956e5 2024-04-29)
ubt :: testbin ‹master› % cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/testbin`
[1, 123, 3]

Is it intended, or something has changed from the language itself.

@c4p1t4n
Copy link

c4p1t4n commented May 7, 2024

i'm new in rust too, i compiled this code and received the same result that u. I know that the language is retro-compatibility. In my mind that is just a mistake in documentation. I think that the correct code to get an error is

fn main() {
  let mut a = vec![1, 2, 3];
  let b = a;  //  &mut borrow of `a` starts here
  // some code
    b[1] = 123;

  println!("{:?}", a); // trying to access `a` as a shared borrow, so giving an error
}          

In this case the ownership of a is moved to b, and you can't access a in the print line

@dumindu dumindu added the bug label May 12, 2024
@dumindu
Copy link
Contributor

dumindu commented May 12, 2024

@trichimtrich Thanks for highlighting this. In 2018-2022, Rust ecosystem added more improvements for lifetimes, especially to improve DX; https://users.rust-lang.org/t/multiple-mutable-references-in-scope/20936 but the given code block was written in 2017. I think I need to double check NLLs in the examples of those sections and update them.

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

No branches or pull requests

3 participants