Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

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:

text
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() {
}