-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.rs
More file actions
62 lines (48 loc) · 1.2 KB
/
oop.rs
File metadata and controls
62 lines (48 loc) · 1.2 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
51
52
53
54
55
56
57
58
59
60
61
62
trait AbstractAnimal {
fn kind(&self) -> &str { "any generic animol" }
fn sound(&self) -> &str { "brrr" }
}
trait MakeSound {
fn speak(&self) {
println!("kek");
}
}
struct Animal {}
struct Dog {}
struct Human {}
struct Cat {}
impl<T> MakeSound for T
where T: AbstractAnimal
{
fn speak(&self) {
println!("{} goes {}", self.kind(), self.sound());
}
}
impl MakeSound for Human {}
impl AbstractAnimal for Animal {}
impl AbstractAnimal for Dog {
fn kind(&self) -> &str { "dog" }
fn sound(&self) -> &str { "woof" }
}
impl AbstractAnimal for Cat {
fn kind(&self) -> &str { "cat" }
fn sound(&self) -> &str { "meow" }
}
fn main() {
let dog = Dog {};
let cat = Cat {};
let hummus = Human {};
let animol = Animal {};
dog.speak();
cat.speak();
animol.speak();
print!("except the stupid human that talks and says ");
hummus.speak();
println!("I eat mostly {}s and {}s but ok with {}", cat.kind(), dog.kind(), animol.kind());
}
// output:
// dog goes woof
// cat goes meow
// any generic animol goes brrr
// except the stupid human that talks and says kek
// I eat mostly cats and dogs but ok with any generic animol