Skip to content

Commit 4c64b81

Browse files
Update from comments
1 parent 283a0a0 commit 4c64b81

4 files changed

Lines changed: 46 additions & 49 deletions

File tree

courses/rust_essentials/060_references/01_shared.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Shared References
2020
2121
.. note::
2222

23-
You can have unlimited shared references to the same data at the same time
23+
Unlimited shared references to the *same* data can exist at the *same* time!
2424

25-
-----------------------------------
26-
Using Reference (Accessing data)
27-
-----------------------------------
25+
----------------------------------
26+
Using Reference (Accessing Data)
27+
----------------------------------
2828

2929
- The :rust:`*` operator **dereferences** the reference to read the value
3030

@@ -43,7 +43,7 @@ Automatic Dereferencing for Field Access
4343
------------------------------------------
4444

4545
- Use :rust:`.` for field access
46-
- No C++ style :cpp:`->`
46+
- No :cpp:`->` operator (unlike C++)
4747

4848
.. code:: rust
4949
@@ -59,9 +59,9 @@ Automatic Dereferencing for Field Access
5959

6060
:command:`ref x: 3, ref y: 5`
6161

62-
-------------------------
63-
Reference Reassignement
64-
-------------------------
62+
------------------------
63+
Reference Reassignment
64+
------------------------
6565

6666
.. code:: rust
6767

courses/rust_essentials/060_references/02_exclusive.rst

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
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
----------------------
6766
Binding vs Reference
@@ -94,9 +93,9 @@ The four quadrants of Rust's reference system
9493
- Mutable
9594
- Mutable
9695

97-
---------------------------------
96+
-----------------------------
9897
The "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
---------------------------------
129128
The "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
---------------------------------
153152
The "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
---------------------------------------
177176
The "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

courses/rust_essentials/060_references/03_slices.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,17 @@ Fat Pointer
7171
7272
.. note::
7373

74-
Creating a slice is **O(1)** ; it takes the same constant time whether the original array has 4 elements or 4 million.
74+
Creating a slice is **O(1)** - it takes the same constant time whether the original array has 4 elements or 4 million.
7575

7676
------------------
7777
&str vs "String"
7878
------------------
7979

8080
- :rust:`&str`: **String slice**, immutable reference to UTF-8 encoded bytes
81-
- Similar to :rust:`&[u8]`
8281
- Fixed length (cannot grow or shrink)
8382
- String literals (:rust:`"Hello"`) are :rust:`&str`
8483
- :rust:`String`: Buffer of UTF-8 encoded bytes
8584
- Allocated on the heap, can grow or shrink
86-
- Similar to C++ :cpp:`std::string`
8785

8886
.. code:: rust
8987

courses/rust_essentials/060_references/99_summary.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ What We Covered
1111
- Allow read-only access to value
1212
- Implemented as safe pointers
1313

14-
- **Exclusive References** (:rust:`&mut T`)
14+
- **Mutable References** (:rust:`&mut T`)
1515

1616
- Allow mutable access
17-
- Only one exclusive reference allowed at a time
17+
- Only one mutable reference allowed at a time
1818

1919
- **Safety**
2020
- References are never null and are always valid

0 commit comments

Comments
 (0)