Skip to content

Commit 733f740

Browse files
Fixes per MR review
1 parent 61be434 commit 733f740

5 files changed

Lines changed: 53 additions & 19 deletions

File tree

courses/rust_essentials/120_std_traits/03-operators.rst

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,27 @@ Operator Overloading
2020
Example
2121
---------
2222

23+
Define an implementation for :rust:`Inches` that returns a number in :rust:`Feet`
24+
2325
.. code:: rust
2426
25-
#[derive(Debug, Copy, Clone)]
26-
struct Point {
27-
x: i32,
28-
y: i32,
29-
}
27+
struct Feet(f64);
28+
struct Inches(f64);
3029
31-
impl std::ops::Add for Point {
32-
type Output = Self;
30+
impl std::ops::Add for Inches {
31+
type Output = Feet;
3332
34-
fn add(self, other: Self) -> Self {
35-
Self { x: self.x + other.x, y: self.y + other.y }
33+
fn add(self, rhs: Self) -> Self::Output {
34+
Feet((self.0 + rhs.0) / 12.0)
3635
}
3736
}
3837
3938
fn main() {
40-
let p1 = Point { x: 10, y: 20 };
41-
let p2 = Point { x: 100, y: 200 };
42-
println!("{p1:?} + {p2:?} = {:?}", p1 + p2);
39+
let measure1 = Inches(10.0);
40+
let measure2 = Inches(32.0);
41+
42+
println!("{} inches + {} inches", measure1.0, measure2.0);
43+
44+
let feet = measure1 + measure2;
45+
println!("= {} feet", feet.0);
4346
}
44-
45-
.. note::
46-
47-
:rust:`Output` is an associated type — implementer decides the result type

courses/rust_essentials/120_std_traits/04-type_conversions.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,33 @@ Example
4343
let one: i16 = true.into();
4444
let bigger: i32 = 123_i16.into();
4545
46+
------------------
47+
"From" vs "Into"
48+
------------------
49+
50+
* Implementation
51+
52+
* If you implement :rust:`From`, you automatically get :rust:`Into`
53+
54+
* Specificity
55+
56+
* :rust:`From` specifies both source and destination
57+
58+
.. code:: rust
59+
60+
let result = Feet::from(measure);
61+
62+
* :rust:`result` is in :rust:`Feet`
63+
64+
* :rust:`Into` is called on an object
65+
66+
* Compiler might not know the source
67+
* Need to supply hints
68+
69+
.. code:: rust
70+
71+
// What do we want 'target' to be?
72+
let target = source.into();
73+
74+
// You have to help it:
75+
let target: Feet = source.into();

courses/rust_essentials/120_std_traits/05-casting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Casting
66
Conversion Between Numbers and Pointers
77
-----------------------------------------
88

9-
* Casting traits help interpet/transform values
9+
* Casting traits help interpret/transform values
1010

1111
* Convert between numeric and pointer types
1212

courses/rust_essentials/120_std_traits/06-io_traits.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Reading and Writing via Trait
1414
* Why It Matters
1515

1616
* Central to Rust’s I/O ecosystem (files, network, buffers)
17-
* Trait-based so many types can be read from / written to generically
17+
* Trait-based
18+
19+
* Many types can be read from / written to generically
1820

1921
--------------------
2022
Practical Patterns

courses/rust_essentials/120_std_traits/07-default_values.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ Example
3535

3636
.. code:: rust
3737
38-
let c = Config::default();
38+
let example = Config::default();
39+
println!("Defaults: {example:?}");
3940
4041
* :rust:`default` uses sensible default values for :rust:`port`,
4142
:rust:`host`, and :rust:`debug`
4243

44+
:command:`Defaults: Config { port: 0, host: "", debug: false }`
45+
4346
* Using *struct update syntax*
4447

4548
.. code:: rust

0 commit comments

Comments
 (0)