Skip to content

Commit 8a97e94

Browse files
traits
1 parent 90a5ebe commit 8a97e94

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/_27_yo_trait.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod _23_yo_pattern_matching;
1717
mod _24_yo_type_alias;
1818
mod _25_yo_module;
1919
mod _26_yo_crate;
20+
mod _27_yo_trait;
2021
mod _2_data_types;
2122
mod _3_numbers;
2223
mod _4_operators_comparison;
@@ -71,4 +72,5 @@ fn main() {
7172
_24_yo_type_alias::main();
7273
_25_yo_module::main();
7374
_26_yo_crate::main();
75+
_27_yo_trait::main();
7476
}

0 commit comments

Comments
 (0)