Exercise 1 · Chapter: TraitsRequired+50 XP
Animal Speak
Define a trait Speak with a method sound(&self) -> &str. Implement it for Dog (returns "Woof!") and Cat (returns "Meow!"). Print both.
Expected output:
Dog says: Woof! Cat says: Meow!
Hints:
- Define
trait Speak { fn sound(&self) -> &str; } - Create empty structs:
struct Dog;andstruct Cat; - Implement
Speakfor each withimpl Speak for Dog { ... }
rust
fn main() {
}
Output
Run your code to see the output here.