Skip to content

Commit 6ba92ec

Browse files
committed
wip
1 parent 2f58438 commit 6ba92ec

File tree

8 files changed

+109
-333
lines changed

8 files changed

+109
-333
lines changed

content/lessons/05_types_reasoning/basic_trait_display.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

content/lessons/05_types_reasoning/generics.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
use std::fmt::Debug;
44

5-
// generic enums
5+
// Generic enums.
66
enum OurOption<T> {
77
Some(T),
88
None,
99
}
1010

11-
// generic structs
11+
// Generic structs.
1212
struct Tuple2<T, U> {
1313
x: T,
1414
y: U,
1515
}
1616

17-
// generic implementation
17+
// Generic implementation.
1818
impl<T, U> Tuple2<T, U> {
1919
fn new(x: T, y: U) -> Self {
2020
Self { x, y }
@@ -26,7 +26,7 @@ struct Pair<T> {
2626
y: T,
2727
}
2828

29-
// conditional implementation
29+
// Conditional implementation.
3030
impl<T: PartialOrd + Copy> Pair<T> {
3131
fn largest(&self) -> T {
3232
if self.x > self.y {
@@ -37,7 +37,7 @@ impl<T: PartialOrd + Copy> Pair<T> {
3737
}
3838
}
3939

40-
// alternative syntax
40+
// Alternative syntax.
4141
impl<T> Pair<T>
4242
where
4343
T: PartialOrd + Copy,
@@ -51,19 +51,24 @@ where
5151
}
5252
}
5353

54-
// Here information about the concrete underlying type is preserved.
54+
// The information about the concrete underlying type is preserved.
55+
// If I call it with a `String`, then I get back a `String`.
5556
fn cloning_machine<T: Clone + Debug>(item: &T) -> T {
5657
item.clone()
5758
}
5859

59-
// Here information about the concrete underlying type is erased.
60+
// The information about the concrete underlying type is erased.
6061
// We can only either format or clone the result.
61-
fn erasing_cloning_machine1(item: &(impl Clone + Debug)) -> impl Clone + Debug {
62+
// If I call it with a `String`, then I'll only know that the return type
63+
// implements `Clone + Debug`.
64+
fn erasing_cloning_machine2<T: Clone + Debug>(item: &T) -> impl Clone + Debug {
6265
item.clone()
6366
}
6467

65-
// Ditto.
66-
fn erasing_cloning_machine2<T: Clone + Debug>(item: &T) -> impl Clone + Debug {
68+
// The returned type behaves exactly the same as above (it's the same type, after all)
69+
// and the function has the same requirements for the `item` argument.
70+
// But inside the implementation of the function, we can't use `T` (it's not defined anywhere).
71+
fn erasing_cloning_machine1(item: &(impl Clone + Debug)) -> impl Clone + Debug {
6772
item.clone()
6873
}
6974

content/lessons/05_types_reasoning/generics_fun.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Display for Bar {
2121

2222
impl Default for Bar {
2323
fn default() -> Self {
24-
Bar // well, we have no other choice
24+
Bar // Well, we have no other choice.
2525
}
2626
}
2727

@@ -30,6 +30,9 @@ impl DefaultishablyPrintable<i32> for Foo {}
3030
impl DefaultishablyPrintable<Bar> for Foo {}
3131

3232
fn main() {
33+
// By typing `Foo as DefaultishablyPrintable<i32>`,
34+
// we tell the compiler to treat this `Foo` struct as
35+
// only a `DefaultishablyPrintable<i32>` trait.
3336
<Foo as DefaultishablyPrintable<i32>>::defaultish_print();
3437
<Foo as DefaultishablyPrintable<Bar>>::defaultish_print();
3538
}

content/lessons/05_types_reasoning/impl_trait.rs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)