Skip to content

Commit f901627

Browse files
committed
corrections review 25/02
1 parent 5d413dd commit f901627

4 files changed

Lines changed: 31 additions & 57 deletions

File tree

courses/rust_essentials/070_structs_and_enums/01_introduction.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ Introduction
66
Topics Covered
77
----------------
88

9-
- **Structs**:
9+
- **Structs**
1010

1111
- Group related data
1212

1313
- Initialization and update syntax
1414

1515
- Named-field vs Tuple forms
1616

17-
- **Enums**:
17+
- **Enums**
1818

1919
- Define distinct options
2020

courses/rust_essentials/070_structs_and_enums/02_structures.rst

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Basics
88

99
- :rust:`struct` creates a type that can hold multiple related values
1010

11-
- Visually similar to :c:`struct` in C/C++, or :ada:`record` in Ada
11+
- Visually similar to :cpp:`struct` in C/C++ or :ada:`record` in Ada
1212

1313
- Can hold any type that is :dfn:`Sized`
1414

@@ -48,10 +48,9 @@ Nesting Structs
4848
}
4949
struct Car {
5050
new: bool,
51-
// power_plant is a component of Car struct
51+
// 'power_plant' is a component of 'Car' struct
5252
power_plant: Engine,
5353
}
54-
5554
5655
----------------------
5756
Beware of Recursion!
@@ -62,26 +61,24 @@ Beware of Recursion!
6261

6362
.. code:: rust
6463
65-
// RussianDoll size = size of u8 + size of itself
64+
// 'RussianDoll' size = size of u8 + size of itself
6665
// Size is infinite
6766
struct RussianDoll {
6867
size: u8,
69-
// ...another RussianDoll!" (Infinite recursion)
68+
// ...another 'RussianDoll'! (Infinite recursion)
7069
inner_doll: RussianDoll,
7170
}
7271
7372
.. container:: latex_environment footnotesize
7473

7574
:error:`error[E0072]: recursive type 'RussianDoll' has infinite size`
76-
77-
78-
75+
7976
-----------------------
8077
Struct Initialization
8178
-----------------------
8279

8380
- Initialization of every field is **mandatory**
84-
- No implicit default values
81+
- No implicit default values
8582

8683
.. code:: rust
8784
@@ -98,7 +95,6 @@ Struct Initialization
9895
sign_in_count: attempt_number,
9996
logged_in: true;
10097
};
101-
10298
10399
--------------------------------
104100
Field Initialization Shorthand
@@ -128,7 +124,6 @@ Field Initialization Shorthand
128124
logged_in: true;
129125
};
130126
131-
132127
------------------------
133128
Struct Update Operator
134129
------------------------
@@ -139,7 +134,7 @@ Struct Update Operator
139134
- Base instance can't be followed by a comma
140135
- Must be at the end of the declaration
141136

142-
.. warning:: Fields are *moved* if their type (eg :rust:`String`) don't implement the :rust:`copy` trait
137+
.. warning:: Fields are *moved* if their type (e.g., :rust:`String`) don't implement the :rust:`copy` trait
143138

144139
.. code:: rust
145140
@@ -151,19 +146,15 @@ Struct Update Operator
151146
font_size: 14,
152147
active: false,
153148
};
154-
// only change 'active' to true in 'set_1'
149+
// Only change 'active' to true in 'set_1'
155150
let set_1 = Settings {
156151
active: true, // Overridden field
157152
..default_set // Copy all other fields (font_size)
158153
};
159154
let set_2 = Settings {
160155
..default_set // Copy all fields
161156
};
162-
let set_3 = Settings {
163-
..default_set, // ERROR, can't be followed by a comma
164-
};
165-
166-
:error:`error: cannot use a comma after the base struct`
157+
167158
168159
---------
169160
Mutable
@@ -189,7 +180,6 @@ Mutable
189180
is_napping: false,
190181
};
191182
192-
193183
:error:`error: expected identifier, found keyword 'mut'`
194184

195185
---------------
@@ -205,7 +195,7 @@ Tuple Structs
205195
struct Character(
206196
u64, // Power
207197
i64, // Money
208-
bool, // is good?
198+
bool, // Is good?
209199
);
210200
// How you use it:
211201
let hero = Character(10000, -500, true);
@@ -226,41 +216,34 @@ Value Illusion
226216

227217
- Constructor function call
228218

229-
- This compiles
230-
231-
232219
.. code:: rust
233220
234221
struct Point(i32, i32);
235222
// Creates an alias
236-
let coord = Point; // Point is a function call
237-
238-
223+
let coord = Point; // 'Point' is the constructor function
224+
239225
.. warning::
240226

241227
:rust:`coord` is NOT a variable of type :rust:`Point`. It is an alias for the constructor function call
242228

243-
244-
245229
.. code:: rust
246230
247-
// Calls the alias for Point constructor
231+
// Calls the alias for 'Point' constructor
248232
// Initializes the tuple
249-
let maximum = coord(1,2); // maximum is a Point
233+
let maximum = coord(1,2); // 'maximum' is a 'Point'
250234
251235
- This doesn't compile
252236

253237
.. code:: rust
254238
255-
// Calls the Point constructor
239+
// Calls the 'Point' constructor
256240
// No initilization because of missing fields
257241
let coord2 = Point();
258242
259243
.. container:: latex_environment footnotesize
260244

261245
:error:`error[E0061]: this struct takes 2 arguments but 0 arguments were supplied`
262-
263-
246+
264247
-------------------------
265248
Type Safety with Tuples
266249
-------------------------
@@ -281,9 +264,7 @@ Type Safety with Tuples
281264
dimension = coordinates; // ERROR
282265
283266
:error:`error[E0308]: mismatched types`
284-
285-
286-
267+
287268
----------------
288269
Idiom: Newtype
289270
----------------
@@ -292,19 +273,11 @@ Idiom: Newtype
292273

293274
- Used to ensure type safety
294275

295-
296276
.. code:: rust
297277
298-
struct UserId(i64);
299-
struct LapseSecondsDuration(i64);
300-
301-
let mut my_id = UserId(15);
302-
let mut my_time = Duration(53);
303-
304-
- :rust:`UserId` and :rust:`Duration` are both :rust:`i64` but can't assign one to the other
305-
306-
.. code:: rust
307-
308-
// ERROR mismatched types
309-
my_id = my_time;
278+
struct Feet(i32);
279+
struct Inches(i32);
310280
281+
let mut distance = Feet(12) + Inches(3);
282+
283+
:error:`error[E0369]: cannot add 'Inches' to 'Feet'`

courses/rust_essentials/070_structs_and_enums/03_enums.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ Enum Initialization
7777
Idiom: State Machine
7878
----------------------
7979

80-
- Each variant is **mutually exclusive**
80+
Each variant is **mutually exclusive**
8181

8282
.. code:: rust
8383
84-
// represent distinct states of a network connection
84+
// Represent distinct states of a network connection
8585
enum ConnectionState {
86-
Idle, // Unit variant (no data)
86+
// Unit variant (no data)
87+
Idle,
88+
// Struct variant (contains data)
8789
Connected {
88-
// Struct variant (contains data)
8990
session_id: u64,
9091
curr_ip: IpAddressV4,
9192
},

courses/rust_essentials/070_structs_and_enums/99_summary.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ What We Learned
1212

1313
- Full field initialization is mandatory
1414

15-
- Uses the Struct Update Operator (..) for easy copying
15+
- Uses the Struct Update Operator (:rust:`..`) for easy copying
1616

17-
- Includes Named-field structs, Tuple structs, and the newtype Idiom
17+
- Includes Named-field structs, Tuple structs, and the *newtype* idiom
1818

1919
- **Enums**
2020

0 commit comments

Comments
 (0)