Skip to content

Commit 573cdb0

Browse files
committed
FCorrections after review- but needs checking
1 parent f108678 commit 573cdb0

File tree

5 files changed

+100
-93
lines changed

5 files changed

+100
-93
lines changed

courses/rust_essentials/070_structs_and_enums.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
***********************
2-
070_structs_and_enums
2+
Structs And Enums
33
***********************
44

55
.. container:: PRELUDE BEGIN

courses/rust_essentials/070_structs_and_enums/01_introduction.rst

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,35 @@ Topics Covered
88

99
- **Structs**: Grouping Related Data
1010

11-
- Initialization & update syntax
11+
- Initialization and update syntax
1212

13-
- Named vs Tuple forms
13+
- Named-field vs Tuple forms
1414

15-
- **Enums**: Defining Distinct Options
15+
- **Enums**: defining distinct options
1616

17-
- Variants holding Data
17+
- Variants holding data
1818

1919
- State machines
2020

21-
- **Type Aliases**
22-
23-
- Renaming types for readability
2421

2522

2623
--------------
2724
Introduction
2825
--------------
2926

30-
- :rust:`struct` and :rust:`enum` are user defined types
27+
- :rust:`struct` and :rust:`enum` are user-defined types
3128

3229
- Create custom data structures
3330
- Bundle related pieces of information together
3431
- Define their behavior
3532

3633
.. code:: rust
3734
38-
struct Point {
39-
x_coordinate: f64,
40-
y_coordinate: f64,
41-
z_coordinate: f64,
35+
struct PlayerStats {
36+
level: u16,
37+
health: u32,
38+
is_online: bool,
39+
score: i64,
4240
}
4341
enum Direction {
4442
North,

courses/rust_essentials/070_structs_and_enums/02_structures.rst

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
Structs
33
=========
44

5-
------------
6-
The Basics
7-
------------
5+
--------
6+
Basics
7+
--------
88

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

1111
- Visually similar to a struct in C/C++, or a record in Ada
1212

13-
- Can hold any type that is :dfn:`sized`
13+
- Can hold any type that is :dfn:`Sized`
1414

15-
- its size is known at compile time
15+
- Size is known at compile time
1616

1717
- Fields accessed via dot notation
18+
19+
- Called **named-field struct**
1820

1921

2022
.. code:: rust
@@ -30,14 +32,13 @@ The Basics
3032
}
3133
3234
if myself.number_of_messages > 250 {
33-
println!("You post too much !");
35+
println!("You post too much!");
3436
}
3537
36-
------------------------
37-
Nesting inside structs
38-
------------------------
38+
-----------------
39+
Nesting structs
40+
-----------------
3941

40-
- Can hold :rust:`struct`
4142

4243
.. code:: rust
4344
@@ -46,20 +47,18 @@ Nesting inside structs
4647
fuel_type: String,
4748
}
4849
struct Car {
49-
make: String,
50-
model: String,
5150
new: bool,
5251
// power_plant is a component of Car struct
5352
power_plant: Engine,
5453
}
5554
5655
57-
------------------------
56+
----------------------
5857
Beware of Recursion!
59-
------------------------
58+
----------------------
6059

61-
- can't be **recursive**
62-
- Would make it a **not sized** type
60+
- Can't be **recursive**
61+
- Type would not be **not sized**
6362

6463
.. code:: rust
6564
@@ -69,16 +68,20 @@ Beware of Recursion!
6968
size: u8,
7069
// ...another RussianDoll!" (Infinite recursion)
7170
inner_doll: RussianDoll,
72-
}
71+
}
72+
73+
.. container:: latex_environment footnotesize
74+
75+
:error:`error[E0072]: recursive type 'RussianDoll' has infinite size`
76+
7377

74-
:error:`error[E0072]: recursive type 'RussianDoll' has infinite size`
7578

7679
-----------------------
7780
Struct Initialization
7881
-----------------------
7982

80-
- Initialization of every field of a :rust:`struct` is **mandatory** when you instantiate it
81-
- There are no implicit default values
83+
- Initialization of every field is **mandatory**
84+
- No implicit default values
8285

8386
.. code:: rust
8487
@@ -93,17 +96,17 @@ Struct Initialization
9396
let user_1 = User {
9497
active: status,
9598
sign_in_count: attempt_number,
96-
logged_in
99+
logged_in: true;
97100
};
98101
99102
100-
----------------------
101-
Field Init Shorthand
102-
----------------------
103+
--------------------------------
104+
Field Initialization Shorthand
105+
--------------------------------
103106

104-
- If a local variable has the same name as the :rust:`struct` field, name could be written only once
107+
- If a variable has the same name as field, name could be written only once
105108
- Compiler automatically expands the variable
106-
- name association and Field Init Shorthand can be used together
109+
- Name association and :dfn:`Field Init Shorthand` can be used together
107110
- No positional association allowed
108111

109112
.. code:: rust
@@ -122,15 +125,15 @@ Field Init Shorthand
122125
active,
123126
// named association is still possible
124127
sign_in_count: sign_in_count,
125-
logged_in,
128+
logged_in: true;
126129
};
127130
128131
129132
------------------------
130133
Struct Update Operator
131134
------------------------
132135

133-
- Allows creation of :rust:`struct` based on another instance via '..' operator
136+
- Creation of :rust:`struct` based on another instance via '..' operator
134137
- Specify values only for the fields that needs to change
135138
- All unspecified fields are copied from the instance following the '..'
136139
- Base instance can't be followed by a comma
@@ -149,13 +152,13 @@ Struct Update Operator
149152
// only change 'active' to true in 'set_1'
150153
let set_1 = Settings {
151154
active: true, // Overridden field
152-
..default_set // Copy all other fields (font_size)
155+
..default_set // Copy all other fields (font_size)
153156
};
154157
let set_2 = Settings {
155-
..default_set // Copy all fields
158+
..default_set // Copy all fields
156159
};
157160
let set_3 = Settings {
158-
..default_set, // ERROR, can't be follow by a comma
161+
..default_set, // ERROR, can't be followed by a comma
159162
};
160163
161164
:error:`error: cannot use a comma after the base struct`
@@ -191,8 +194,8 @@ Mutable
191194
Tuple Structs
192195
---------------
193196

194-
- Like regular :rust:`struct`, can hold any type that is **sized**
195-
- Useful to give a structure a specific type name without naming all of its fields
197+
- Like Named-field :rust:`struct`, can hold any type that is **Sized**
198+
- Useful to give a structure a specific name without naming any fields
196199
- First element of a tuple is 0 not 1
197200

198201
.. code:: rust
@@ -202,29 +205,33 @@ Tuple Structs
202205
i64, // Money
203206
bool, // is good?
204207
);
205-
206208
// How you use it:
207209
let hero = Character(10000, -500, true);
208210
println!("Power level is : {}", hero.0);
209211
println!("Money is : {}", hero.1);
210212
213+
println!("out of bound is : {}", hero.3);
214+
215+
:error:`error[E0609]: no field `3` on type 'Character'`
216+
211217
-------------------------
212218
Type Safety with Tuples
213219
-------------------------
214220

215-
- Name differentiates types not their definition,
216-
- Tuple structs with the same definition are different types
221+
- **Name** differentiates types
222+
- Not their definition
223+
- Tuple structs with the same definition are different types
217224

218225

219226
.. code:: rust
220227

221228
struct Point(i32, i32);
222229
struct Size(i32, i32);
223230

224-
let p = Point(10, 20);
225-
let mut s = Size(30, 40);
231+
let coordinates = Point(10, 20);
232+
let mut dimension = Size(30, 40);
226233

227-
s = p; // ERROR
234+
dimension = coordinates; // ERROR
228235

229236
:error:`error[E0308]: mismatched types`
230237

@@ -234,24 +241,24 @@ Type Safety with Tuples
234241
Idiom: NewType
235242
----------------
236243

237-
244+
//JBE
238245
- A :dfn:`newtype` is a tuple :rust:`struct` with a single field.
239246

240247
- Used to ensure type safety
241248

242-
- :rust:`UserId` and :rust:`Duration` are both :rust:`i64` but can't assign one to the other
243249

244250
.. code:: rust
245251
246-
// The newtype (compiler-checked type safety)
247252
struct UserId(i64);
248253
struct LapseSecondsDuration(i64);
249254
250255
let mut my_id = UserId(15);
251256
let mut my_time = Duration(53);
257+
258+
- :rust:`UserId` and :rust:`Duration` are both :rust:`i64` but can't assign one to the other
252259

253-
//ERROR incompatible type
260+
// ERROR mismatched types
254261
my_id = my_time;
255-
262+
//JBE
256263

257264

0 commit comments

Comments
 (0)