-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraits.rs
More file actions
50 lines (41 loc) · 1.15 KB
/
traits.rs
File metadata and controls
50 lines (41 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! traits.rs
//!
//! Introduction to Rust traits — the language's way of defining shared behavior.
//! Shows trait definition, implementation, and trait bounds.
fn main() {
let dog: Dog = Dog { name: String::from("Buddy") };
let cat: Cat = Cat { name: String::from("Whiskers") };
println!("Dog says: {}", dog.make_sound());
println!("Cat says: {}", cat.make_sound());
// Using a trait bound function
print_animal_sound(&dog);
print_animal_sound(&cat);
}
// 1. Define a trait (like an interface)
trait Animal {
fn make_sound(&self) -> String;
fn describe(&self) -> String {
String::from("This is an animal")
}
}
// 2. Implement the trait for a concrete type
struct Dog {
name: String,
}
impl Animal for Dog {
fn make_sound(&self) -> String {
format!("{} says Woof!", self.name)
}
}
struct Cat {
name: String,
}
impl Animal for Cat {
fn make_sound(&self) -> String {
format!("{} says Meow!", self.name)
}
}
// 3. Function that accepts any type that implements the Animal trait
fn print_animal_sound<T: Animal>(animal: &T) {
println!("Animal sound: {}", animal.make_sound());
}