-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
40 lines (35 loc) · 832 Bytes
/
main.rs
File metadata and controls
40 lines (35 loc) · 832 Bytes
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
trait AnimalSound {
fn make_voice(&self) {
println!("animal's voice!");
}
fn get_name(&self) -> String;
}
struct Dog {
name: String,
voice: String
}
struct Cat {
name: String,
voice: String
}
impl AnimalSound for Dog {
fn make_voice(&self) {
println!("Dog's voice: {}", self.voice);
}
fn get_name(&self) -> String {
format!("Dog's name: {}", self.name)
}
}
impl AnimalSound for Cat {
fn get_name(&self) -> String {
format!("Cat's name: {}", self.name)
}
}
fn main() {
let dog = Dog { name: "karabas".to_string(), voice: "Woof!".to_string() };
println!("{}", dog.get_name());
dog.make_voice();
let cat = Cat { name: "boncuk".to_string(), voice: "Meow!".to_string() };
println!("{}", cat.get_name());
cat.make_voice();
}