From 386757e69757ac5747bf41df2c2884b6cb66b4a1 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Fri, 7 Feb 2025 11:15:00 -0800 Subject: [PATCH] Add note that borrock error can be triggered by direct mutation (#2629) --- src/borrowing/borrowck.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/borrowing/borrowck.md b/src/borrowing/borrowck.md index 871321bb..4d53987c 100644 --- a/src/borrowing/borrowck.md +++ b/src/borrowing/borrowck.md @@ -51,6 +51,10 @@ fn main() { same point. It does not matter where the reference is dereferenced. - The above code does not compile because `a` is borrowed as mutable (through `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 make the code compile. - After that change, the compiler realizes that `b` is only ever used before the