@@ -33,10 +33,6 @@ fn main() {
3333}
3434
3535// ⭐️ 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.
4036```
4137
4238## Impls & traits, without default methods
@@ -67,6 +63,9 @@ fn main() {
6763}
6864
6965// 🔎 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.
7069```
7170
7271## Impls, traits & default methods
@@ -122,13 +121,13 @@ fn main() {
122121trait From <T > {
123122 fn from (T ) -> Self ;
124123}
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+ }
132131// Should specify after the trait name like generic functions
133132```
134133
@@ -139,13 +138,13 @@ trait Person {
139138 fn full_name (& self ) -> String ;
140139}
141140
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+ }
145144
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+ }
149148```
150149
151150## Trait objects
@@ -163,20 +162,22 @@ trait GetSound {
163162struct Cat {
164163 sound : String ,
165164}
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 ()
170169 }
170+ }
171171
172172struct Bell {
173173 sound : String ,
174174}
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 ()
179179 }
180+ }
180181
181182
182183fn make_sound <T : GetSound >(t : & T ) {
0 commit comments