22
33use std:: fmt:: Debug ;
44
5- // generic enums
5+ // Generic enums.
66enum OurOption < T > {
77 Some ( T ) ,
88 None ,
99}
1010
11- // generic structs
11+ // Generic structs.
1212struct Tuple2 < T , U > {
1313 x : T ,
1414 y : U ,
1515}
1616
17- // generic implementation
17+ // Generic implementation.
1818impl < 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.
3030impl < 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.
4141impl < T > Pair < T >
4242where
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`.
5556fn 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
0 commit comments