Skip to content

Commit 3ea954a

Browse files
Merge branch 'mr/387-rework-rust-essentials-module-generics' into 'master'
Resolve "Rework Rust Essentials module: Generics" Closes #387 See merge request feng/training/material!519
2 parents 910db92 + 62095c9 commit 3ea954a

13 files changed

Lines changed: 494 additions & 410 deletions

courses/rust_essentials/100_generics.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ Generics
3232

3333
.. container:: PRELUDE END
3434

35-
.. include:: 100_generics/01_generic_functions.rst
36-
.. include:: 100_generics/02_generic_data.rst
37-
.. include:: 100_generics/03_generic_traits.rst
38-
.. include:: 100_generics/04_trait_bounds.rst
39-
.. include:: 100_generics/05_impl_trait.rst
40-
.. include:: 100_generics/06_dyn_trait.rst
41-
.. include:: 100_generics/99_lab.rst
35+
.. include:: 100_generics/01_introduction.rst
36+
.. include:: 100_generics/02_generics.rst
37+
.. include:: 100_generics/03_constraints_and_properties.rst
38+
.. include:: 100_generics/04_generic_traits_and_constants.rst
39+
.. include:: 100_generics/98_lab.rst
40+
.. include:: 100_generics/99_summary.rst

courses/rust_essentials/100_generics/01_generic_functions.rst

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
----------------
6+
Topics Covered
7+
----------------
8+
9+
- **Generic Type Parameter**
10+
11+
- Definition and instantiation
12+
13+
- Define multiple generic type
14+
15+
- **Constraints and Properties**
16+
17+
- Traits add functionalities
18+
19+
- And restrictions
20+
21+
- **Generic Traits and Constants**
22+
23+
- Define generic constructs over traits and numbers
24+
25+
26+
-------------------------
27+
The Notion of a Pattern
28+
-------------------------
29+
30+
* Sometimes algorithms can be abstracted from types and subprograms
31+
32+
.. code:: Rust
33+
34+
fn swap_int(l: i32, r: i32) -> (i32, i32) {
35+
(right, left)
36+
}
37+
38+
fn swap_float(l: f64, r: f64) -> (f64, f64) {
39+
(right, left)
40+
}
41+
42+
* A common pattern could be extracted, with only some parts to replace
43+
44+
.. code:: Rust
45+
46+
fn swap (l: a_type, r: a_type) -> (a_type, a_type) {
47+
(r, l)
48+
}

courses/rust_essentials/100_generics/02_generic_data.rst

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
==========
2+
Generics
3+
==========
4+
5+
-------------------
6+
Generic Data Type
7+
-------------------
8+
9+
- Used to parameterize an object
10+
11+
- Declared using :rust:`<>`
12+
13+
- Can take any identifier name
14+
15+
- Conventionally called :rust:`T`
16+
17+
.. code:: rust
18+
19+
fn swap<T> (l: T, r: T) -> (T, T) {
20+
(r, l)
21+
}
22+
23+
- :rust:`T` (:dfn:`generic type parameter`) means :rust:`swap` can wrap any type
24+
25+
- :rust:`swap<i32>`, :rust:`swap<MyOwnType>`, etc.
26+
27+
------------
28+
Be Generic
29+
------------
30+
31+
- Constructs that can be made generic
32+
33+
.. container:: latex_environment scriptsize
34+
35+
.. list-table::
36+
:header-rows: 1
37+
38+
* - **Constructs**
39+
- **Example Syntax**
40+
- **Purpose**
41+
42+
* - *Functions*
43+
- :rust:`fn logic<T>(arg: T)`
44+
- Logic that works on multiple types
45+
46+
47+
* - *Structs*
48+
- :rust:`struct Container<T>(T)`
49+
- Data structures that hold any type
50+
51+
52+
* - *Enums*
53+
- :rust:`enum Choice<T> { A(T), B }`
54+
- Variants that can wrap different data
55+
56+
57+
* - *Traits*
58+
- :rust:`trait Behavior<T>`
59+
- Defining interfaces with generic inputs
60+
61+
* - *Type Aliases*
62+
- :rust:`type Res<T> = Result<GenT>`
63+
- Simplifying complex generic names
64+
65+
- Examples
66+
67+
.. code:: rust
68+
69+
enum LaundryStatus<T> {
70+
SoakingInWater(T),
71+
SpinningViolently(T),
72+
}
73+
type Laundry<T> = LaundryStatus<T>;
74+
75+
76+
----------------
77+
Type Inference
78+
----------------
79+
80+
- Any **Sized** type can be used as the type argument
81+
82+
.. code:: rust
83+
84+
// Definition: 'T' is a placeholder for ANY type
85+
fn encourage<T>(item: T) -> T {
86+
println!("You're doing great, little value!");
87+
item
88+
}
89+
90+
- Type is **inferred** at compile-time from the context
91+
92+
.. code:: rust
93+
94+
// With an integer
95+
let points = encourage(100);
96+
97+
// With a string
98+
let name = encourage("Rustacean");
99+
100+
101+
102+
-----------------------
103+
Multiple Generic Types
104+
-----------------------
105+
106+
**Constructs can have multiple generic data types**
107+
108+
.. code:: rust
109+
110+
struct Point<T, U> {
111+
x: T,
112+
y: U,
113+
}
114+
115+
let both_integer = Point { x: 5, y: 10 };
116+
let both_float = Point { x: 1.0, y: 4.0 };
117+
let integer_and_float = Point { x: 5, y: 4.0 };
118+
119+
120+
--------------
121+
Type Aliases
122+
--------------
123+
124+
- Can be used to rename types and generic parameters
125+
126+
.. code:: rust
127+
128+
// 'Item' and 'Label' are generic parameters
129+
struct LargeShippingUnit<Item, Label>(Item, Label);
130+
type LargeCrate<T, U> = LargeShippingUnit<T, U>;
131+
132+
- Can *specify* the generic type
133+
134+
- **Partially**
135+
136+
.. code:: rust
137+
138+
struct Animal;
139+
type AnimalCrate<U> = LargeCrate<Animal, U>;
140+
141+
- **Totally**
142+
143+
.. code:: rust
144+
145+
struct Environment;
146+
type Biome = LargeCrate<Animal, Environment>;

0 commit comments

Comments
 (0)