File tree Expand file tree Collapse file tree
courses/rust_essentials/120_std_traits Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,28 +20,27 @@ Operator Overloading
2020Example
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
Original file line number Diff line number Diff 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();
Original file line number Diff line number Diff line change 66Conversion 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
Original file line number Diff line number Diff 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--------------------
2022Practical Patterns
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments