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----------------------
5756Beware 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-----------------------
8077Struct 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--------------------------------
104100Field Initialization Shorthand
@@ -128,7 +124,6 @@ Field Initialization Shorthand
128124 logged_in: true;
129125 };
130126
131-
132127------------------------
133128Struct 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---------
169160Mutable
@@ -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-------------------------
265248Type 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----------------
288269Idiom: 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' `
0 commit comments