1

I was reading the old rust documentation (the one that shipped with rust 1.3) and found this example. For the convenience of reading I am quoting the example here as well:

fn main() {
    let mut x = 5;

    let y = &mut x;

    *y += 1;

    println!("{}", x);
}

It said if I try to build it, it will throw an error such:

error: cannot borrow `x` as immutable because it is also borrowed as mutable
    println!("{}", x);
                   ^

But I could compile and run it without any error. Was there any major version migration that eliminated this kind of error message?

ino
  • 1,002
  • 1
  • 8
  • 25
  • 1
    Perhaps the answer is given in that "duplicate post", but that post deals with some advanced topics, and it's clear to me that this post is a beginner's post. The likelyhood of user @ino understanding his/her own issue by reading the other post is very slim, as the other post deals with a fair amount of advanced topics. As a result, I don't think this question should be marked as "duplicate" -- at least, not to that post. – J-L Jun 15 '21 at 04:07
  • @J-L except that the OP himself accepted the duplicate (that's what the "Comunity" means in the list of persons who voted to close, and it's also the reason why the question was closed with fewer than 3 close votes). – Jmb Jun 15 '21 at 06:36
  • @Jmb hey, thank you! in fact I was feeling so dumb that I made a duplicate post and accepted it quickly before other people would make the same suggestion for me. – ino Jun 15 '21 at 08:15
  • @J-L ahaha, yes I am beginner, this one knocked me out. could you please explain very shortly why it did not compile before but it does now? Thank you :) – ino Jun 15 '21 at 08:19
  • 1
    @ino, I'll try explaining in the space I'm allotted here: When you write `let y = &mut x;`, you're saying that all access/modifications to `x` will now be done through `y` (or `*y`), and that you will not be able to use `x` until `y` goes out of scope. In older versions of Rust, "end of scope" was at the enclosing `}` brace, same as in C/C++/Java/Perl. But in newer versions of Rust, "end of scope" is essentially at the last usage of the variable. That's why `println!("{}", x);` fails in older versions, but succeeds in newer versions -- because that line is not in `y`'s scope in newer Rust. – J-L Jun 15 '21 at 14:59
  • @J-L thank you so much!! you are a life saver! i wish i did not close the question so i could upvote your answer. again, thanks and have a beautiful day! – ino Jun 15 '21 at 15:03
  • 1
    @ino, you're welcome! (Even though you can't upvote responses, you *can* upvote comments, I believe.) Personally, I wish people weren't so quick to mark duplicates. Your question is a valid beginner question (that I once had myself), and although another post may *refer* to your answer, that doesn't necessarily mean that your question is a duplicate. Stackoverflow is used all over the world by programmers, and the less beginner-friendly we make Rust here, the less likely Rust will become popular. Compare to Python, which has many, many answers, even to duplicate and beginner questions. – J-L Jun 15 '21 at 15:22
  • 1
    @J-L Thank you for your kind and hearty words! I upvoted the comments! – ino Jun 15 '21 at 15:26

0 Answers0