File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change
1
+ trait YoGreet {
2
+ fn greet ( & self ) -> String ;
3
+ }
4
+
5
+ struct YoPerson {
6
+ name : String ,
7
+ }
8
+
9
+ impl YoGreet for YoPerson {
10
+ fn greet ( & self ) -> String {
11
+ format ! ( "Yo, {}!" , self . name)
12
+ }
13
+ }
14
+
15
+ pub fn main ( ) {
16
+ let person = YoPerson {
17
+ name : String :: from ( "Rustacean" ) ,
18
+ } ;
19
+ println ! ( "{}" , person. greet( ) ) ; // Expected : "Yo, Rustacean!"
20
+ }
21
+
22
+ // Real Scenario: Define shared behavior for unrelated types.
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ mod _23_yo_pattern_matching;
17
17
mod _24_yo_type_alias;
18
18
mod _25_yo_module;
19
19
mod _26_yo_crate;
20
+ mod _27_yo_trait;
20
21
mod _2_data_types;
21
22
mod _3_numbers;
22
23
mod _4_operators_comparison;
@@ -71,4 +72,5 @@ fn main() {
71
72
_24_yo_type_alias:: main ( ) ;
72
73
_25_yo_module:: main ( ) ;
73
74
_26_yo_crate:: main ( ) ;
75
+ _27_yo_trait:: main ( ) ;
74
76
}
You can’t perform that action at this time.
0 commit comments