Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: TraitsRequired+50 XP

Default Method

Define a trait Emoji with a default method icon(&self) -> &str that returns "😊". Create two structs: Happy (uses the default) and Cool (overrides it to return "😎"). Print both icons.

Expected output:

Happy: 😊
Cool: 😎

Hints:

  • A default method has a body in the trait: fn icon(&self) -> &str { "😊" }
  • impl Emoji for Happy {} uses the default (empty impl block)
  • impl Emoji for Cool { fn icon(&self) -> &str { "😎" } } overrides it
rust
fn main() {
}
Output
Run your code to see the output here.