Exerpad
🔥 0
0 XP
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; and struct Cat;
  • Implement Speak for each with impl Speak for Dog { ... }
rust
fn main() {
}
Output
Run your code to see the output here.