@@ -33,10 +33,6 @@ fn main() {
33
33
}
34
34
35
35
// ⭐️ Implementation must appear in the same crate as the self type
36
-
37
- // 💡 And also in Rust, new traits can be implemented for existing types even for types like i8, f64 and etc.
38
- // Same way existing traits can be implemented for new types you are creating.
39
- // But we can not implement existing traits into existing types.
40
36
```
41
37
42
38
## Impls & traits, without default methods
@@ -67,6 +63,9 @@ fn main() {
67
63
}
68
64
69
65
// 🔎 Other than functions, traits can contain constants and types.
66
+
67
+ // 💡 And also in Rust, new traits can be implemented for existing types even for types like i8, f64 and etc.
68
+ // Same way existing traits can be implemented for new types you are creating.
70
69
```
71
70
72
71
## Impls, traits & default methods
@@ -122,13 +121,13 @@ fn main() {
122
121
trait From <T > {
123
122
fn from (T ) -> Self ;
124
123
}
125
- impl From < u8 > for u16 {
126
- // ...
127
- }
128
- impl From < u8 > for u32 {
129
- // ...
130
- }
131
-
124
+
125
+ impl From < u8 > for u16 {
126
+ // ...
127
+ }
128
+ impl From < u8 > for u32 {
129
+ // ...
130
+ }
132
131
// Should specify after the trait name like generic functions
133
132
```
134
133
@@ -139,13 +138,13 @@ trait Person {
139
138
fn full_name (& self ) -> String ;
140
139
}
141
140
142
- trait Employee : Person { // Employee inherits from person trait
143
- fn job_title (& self ) -> String ;
144
- }
141
+ trait Employee : Person { // Employee inherits from person trait
142
+ fn job_title (& self ) -> String ;
143
+ }
145
144
146
- trait ExpatEmployee : Employee + Expat { // ExpatEmployee inherits from Employee and Expat traits
147
- fn additional_tax (& self ) -> f64 ;
148
- }
145
+ trait ExpatEmployee : Employee + Expat { // ExpatEmployee inherits from Employee and Expat traits
146
+ fn additional_tax (& self ) -> f64 ;
147
+ }
149
148
```
150
149
151
150
## Trait objects
@@ -163,20 +162,22 @@ trait GetSound {
163
162
struct Cat {
164
163
sound : String ,
165
164
}
166
- impl GetSound for Cat {
167
- fn get_sound ( & self ) -> String {
168
- self . sound . clone ()
169
- }
165
+
166
+ impl GetSound for Cat {
167
+ fn get_sound ( & self ) -> String {
168
+ self . sound . clone ()
170
169
}
170
+ }
171
171
172
172
struct Bell {
173
173
sound : String ,
174
174
}
175
- impl GetSound for Bell {
176
- fn get_sound ( & self ) -> String {
177
- self . sound . clone ()
178
- }
175
+
176
+ impl GetSound for Bell {
177
+ fn get_sound ( & self ) -> String {
178
+ self . sound . clone ()
179
179
}
180
+ }
180
181
181
182
182
183
fn make_sound <T : GetSound >(t : & T ) {
0 commit comments