Skip to content

Commit 169dfa2

Browse files
committed
First batch after rust review 1
1 parent 6effb70 commit 169dfa2

3 files changed

Lines changed: 40 additions & 51 deletions

File tree

courses/rust_essentials/100_generics/01_introduction.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ Topics Covered
1010

1111
- Definition and instantiation
1212

13-
- **Genericity and traits**
14-
15-
- Adding constraints and properties
13+
- **Constraints and properties**
1614

17-
- Generic traits
15+
- **Generic traits**
1816

1917

2018
-------------------------
@@ -37,6 +35,6 @@ The Notion of a Pattern
3735

3836
.. code:: Rust
3937
40-
fn Swap (l: (a_type), r: (a_type)) -> ((a_type), (a_type)) {
38+
fn Swap (l: a_type, r: a_type) -> (a_type, a_type) {
4139
(r, l)
4240
}

courses/rust_essentials/100_generics/02_generics.rst

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,29 @@ Generic Data Type
1616

1717
.. code:: rust
1818
19-
enum Option<T> {
20-
None,
21-
Some(T),
22-
}
19+
fn Swap<T> (l: T, r: T) -> (T, T) {
20+
(r, l)
21+
}
22+
2323

24-
- :rust:`T` (the generic type parameter) means an :rust:`Option` can wrap any type
24+
- :rust:`T` (the generic type parameter) means an :rust:`Swap` can wrap any type
2525

26-
- :rust:`Option<i32>`, :rust:`Option<String>` and others
26+
- :rust:`Swap<i32>`, :rust:`Swap<f64>` etc
2727

28-
- :rust:`None` represents the absence of a value
2928

30-
- :rust:`Some(T)` tuple *variant* that holds a value of type :rust:`T`
3129

3230
------------
3331
Be Generic
3432
------------
3533

36-
Objects that can be made generic
34+
Constructs that can be made generic
3735

3836
.. container:: latex_environment scriptsize
3937

4038
.. list-table::
4139
:header-rows: 1
4240

43-
* - Feature
41+
* - Constructs
4442
- Example Syntax
4543
- Purpose
4644

@@ -67,36 +65,39 @@ Objects that can be made generic
6765
- :rust:`type Res<T> = Result<GenT>`
6866
- Simplifying complex generic names
6967

70-
68+
-- JBE : very all options are present in examples
69+
-- Add Examples ?
7170

72-
---------
73-
Example
74-
---------
7571

76-
- Any type with a known **size** can specify the type argument
72+
----------------
73+
Type Inference
74+
----------------
75+
76+
- Any **Sized**-type can specify the type argument
7777

7878
.. code:: rust
7979
8080
81-
// Definition: T is a placeholder for ANY type
81+
// Definition: 'T' is a placeholder for ANY type
8282
fn encourage<T>(item: T) -> T {
8383
println!("You're doing great, little value!");
8484
item
8585
}
8686
87-
fn main() {
88-
// Usage with an integer
89-
let points = encourage(100);
87+
// Usage with an integer
88+
let points = encourage(100);
9089
91-
// Usage with a string
92-
let name = encourage("Rustacean");
93-
}
90+
// Usage with a string
91+
let name = encourage("Rustacean");
92+
93+
- Type is **infered** at compile-time from the context
94+
9495

9596
-----------------------
9697
Multiple Generic Type
9798
-----------------------
9899

99-
Objects can have multiple generic data types
100+
Constructs can have multiple generic data types
100101

101102
.. code:: rust
102103
@@ -105,11 +106,10 @@ Objects can have multiple generic data types
105106
y: U,
106107
}
107108
108-
fn main() {
109-
let both_integer = Point { x: 5, y: 10 };
110-
let both_float = Point { x: 1.0, y: 4.0 };
111-
let integer_and_float = Point { x: 5, y: 4.0 };
112-
}
109+
let both_integer = Point { x: 5, y: 10 };
110+
let both_float = Point { x: 1.0, y: 4.0 };
111+
let integer_and_float = Point { x: 5, y: 4.0 };
112+
113113
114114
115115

courses/rust_essentials/100_generics/03_generics_and_traits.rst renamed to courses/rust_essentials/100_generics/03_constraints_and_properties.rst

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
====================
2-
Generics and Traits
3-
====================
1+
============================
2+
Constraints and Properties
3+
============================
44

55
--------------
66
Trait Bounds
77
--------------
88

9-
- Generic data :rust:`<T>` is very broad
9+
- Generic data :rust:`<T>` has almost no limitations
1010

11-
- Compiler will restrict what can be done with <T>
12-
- Doesn't know if number or string or anything else
11+
- Compiler will restrict what can be done with :rust:`<T>`
12+
- Doesn't know if it can do math, order or anything else
1313

14-
- Traits are the **fine print** on a generic contract
14+
- Traits are the **fine print** on a generic **contract**
1515

1616
- Ensure the logic only executes on types that "fit" the requirements
1717

@@ -20,19 +20,9 @@ Trait Bounds
2020
.. code:: rust
2121
2222
fn print_it<T: std::fmt::Display>(item: T) {
23-
println!("{}", item); // Only works if T implements Display
23+
println!("{}", item); // Only works if 'T' implements Display
2424
}
2525
26-
- or with a :rust:`where` clause
27-
28-
.. code:: rust
29-
30-
fn complex_function<T, U>(t: T, u: U)
31-
where
32-
T: Display,
33-
U: Debug
34-
{ ... }
35-
3626
3727
--------------------
3828
Adding Constraints
@@ -140,6 +130,7 @@ Multiple Traits
140130

141131
- Using the :rust:`+` operator
142132

133+
- :rust:`where` clause can be used for better visibility
143134

144135
.. code:: rust
145136

0 commit comments

Comments
 (0)