1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-19 22:19:29 +02:00

Add note that borrock error can be triggered by direct mutation (#2629)

This commit is contained in:
Nicole L 2025-02-07 11:15:00 -08:00 committed by GitHub
parent d603faca56
commit 386757e697
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,6 +51,10 @@ fn main() {
same point. It does not matter where the reference is dereferenced. same point. It does not matter where the reference is dereferenced.
- The above code does not compile because `a` is borrowed as mutable (through - The above code does not compile because `a` is borrowed as mutable (through
`c`) and as immutable (through `b`) at the same time. `c`) and as immutable (through `b`) at the same time.
- Note that the intermediate reference `c` isn't necessary to trigger a borrow
conflict. Replace `c` with a direct mutation of `a` and demonstrate that this
produces a similar error. This is because direct mutation of a value
effectively creates a temporary mutable reference.
- Move the `println!` statement for `b` before the scope that introduces `c` to - Move the `println!` statement for `b` before the scope that introduces `c` to
make the code compile. make the code compile.
- After that change, the compiler realizes that `b` is only ever used before the - After that change, the compiler realizes that `b` is only ever used before the