Skip to content

Commit 6ed2796

Browse files
Merge pull request #56 from kidskoding/main
fix: update outdated borrow checker example in chapter 5 of "Rust for C/C++ Programmers" to reflect NLL behavior (fixes #55)
2 parents 68bad3f + a2f318c commit 6ed2796

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

c-cpp-book/src/ch05-data-structures.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,18 @@ fn main() {
7171
let b = &a;
7272
let c = b;
7373
println!("{} {}", *b, *c); // The compiler automatically dereferences *c
74-
// Illegal because b and still are still in scope
75-
// let d = &mut a;
74+
75+
let d = &mut a;
76+
77+
/*
78+
* Uncommenting the line below would be cause the
79+
* program to not compile, because `b` is used
80+
* while the mutable reference `d` is live in the current scope
81+
*
82+
* You cannot have a mutable and immutable reference in use in the same scope
83+
* at the same time!
84+
*/
85+
// println!("{}", *b);
7686
}
7787
let d = &mut a; // Ok: b and c are not in scope
7888
*d = 43;

0 commit comments

Comments
 (0)