11======================
2- Exclusive References
2+ Mutable References
33======================
44
5- --------------------
6- Exclusive References
7- --------------------
5+ -----------------------------------------------
6+ Mutable References (aka Exclusive References)
7+ -----------------------------------------------
88
99- Created with the :rust: `&mut ` operator
10- - Allow changing the value they refer to
11- - Also known as **mutable ** references
12- - No other references (shared or exclusive) can exist simultaneously
13- - An exclusive reference to a type :rust: `T ` has type :rust: `&mut T `
10+ - Allow modifying the value they point to
11+ - Cannot coexist with any other reference
12+ - A mutable reference to a type :rust: `T ` has type :rust: `&mut T `
1413
1514.. code :: rust
1615
@@ -25,11 +24,11 @@ Exclusive References
2524
2625.. note ::
2726
28- You cannot create a :rust: `&mut ` reference to an immutable variable
27+ A :rust: `&mut ` reference cannot be created from an ** immutable ** variable
2928
30- ------------------------------------------
31- How References Use Non-Lexical Lifetimes
32- ------------------------------------------
29+ -----------------------------
30+ Non-Lexical Lifetimes (NLL)
31+ -----------------------------
3332
3433- Case A: :rust: `ref_1 ` is still "active", the compiler won't let :rust: `ref_2 ` exist
3534
@@ -61,7 +60,7 @@ How References Use Non-Lexical Lifetimes
6160
6261.. note ::
6362
64- References live until their last use, not the end of the scope :rust: `{ } `
63+ References live until their last use, not necessarily the end of the scope :rust: `{ } `
6564
6665----------------------
6766Binding vs Reference
@@ -94,9 +93,9 @@ The four quadrants of Rust's reference system
9493 - Mutable
9594 - Mutable
9695
97- ---------------------------------
96+ -----------------------------
9897The "Observer" (let r = &x)
99- ---------------------------------
98+ -----------------------------
10099
101100- *Cannot * point to something else
102101- *Cannot * change the value
@@ -105,25 +104,25 @@ The "Observer" (let r = &x)
105104
106105 let past = 1984;
107106 let future = 2048;
108- let r = &past;
107+ let rf = &past;
109108
110- *r = 1776; // Error
109+ *rf = 1776; // Error
111110
112111 * Generates the following ouput
113112
114- :error: `error[E0594]: cannot assign to '*r ', which is behind a '&'' reference `
113+ :error: `error[E0594]: cannot assign to '*rf ', which is behind a '&'' reference `
115114
116115.. code :: rust
117116
118- r = &future; // Error
117+ rf = &future; // Error
119118
120119 * Generates the following output
121120
122- :error: `error[E0384]: cannot assign twice to immutable variable 'r ' `
121+ :error: `error[E0384]: cannot assign twice to immutable variable 'rf ' `
123122
124123.. note ::
125124
126- "I am stuck with you, and I can only look"
125+ The reference cannot be redirected, nor can the data be modified
127126
128127---------------------------------
129128The "Rebinder" (let mut r = &x)
@@ -136,18 +135,18 @@ The "Rebinder" (let mut r = &x)
136135
137136 let news_a = "War with the North";
138137 let news_b = "War with the South";
139- let mut r = &news_a;
138+ let mut rf = &news_a;
140139
141- r = &news_b;
142- *r = "Peace"; // Error
140+ rf = &news_b;
141+ *rf = "Peace"; // Error
143142
144143 * Generates the following output
145144
146- :error: `error[E0594]: cannot assign to '*r ', which is behind a '&'' reference `
145+ :error: `error[E0594]: cannot assign to '*rf ', which is behind a '&'' reference `
147146
148147.. note ::
149148
150- "I can't touch the contents , but I can look at someone else"
149+ The reference can be redirected , but the data cannot be modified
151150
152151---------------------------------
153152The "Modifier" (let r = &mut x)
@@ -160,18 +159,18 @@ The "Modifier" (let r = &mut x)
160159
161160 let mut room_101 = 0;
162161 let mut room_102 = 0;
163- let r = &mut room_101;
162+ let rf = &mut room_101;
164163
165- *r = 1;
166- r = &mut room_102; // Error
164+ *rf = 1;
165+ rf = &mut room_102; // Error
167166
168167 * Generates the following output
169168
170- :error: `error[E0384]: cannot assign twice to immutable variable 'r ' `
169+ :error: `error[E0384]: cannot assign twice to immutable variable 'rf ' `
171170
172171.. note ::
173172
174- "I am stuck with you , but I can change who you are"
173+ The reference cannot be redirected , but the data can be modified
175174
176175---------------------------------------
177176The "Free Agent" (let mut r = &mut x)
@@ -184,12 +183,12 @@ The "Free Agent" (let mut r = &mut x)
184183
185184 let mut focus = 100;
186185 let mut shame = 0;
187- let mut r = &mut focus;
186+ let mut rf = &mut focus;
188187
189- *r = 0;
190- r = &shame;
191- *r = 999;
188+ *rf = 0;
189+ rf = &shame;
190+ *rf = 999;
192191
193192 .. note ::
194193
195- "I can change the value , and I can move on to something else"
194+ The reference can be redirected , and the data can be modified
0 commit comments