Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

Fix the Display

This code tries to print a Pet struct with {}, but the Display implementation has a bug in the format string. Fix it!

Fix the code so it prints:

text
Buddy the dog

Hint: Check the write! macro — it should use self.name and self.species.

rust
use std::fmt;

struct Pet {
name: String,
species: String,
}

impl fmt::Display for Pet {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} the {}", self.species, self.name)
}
}

fn main() {
let p = Pet {
name: String::from("Buddy"),
species: String::from("dog"),
};
println!("{}", p);
}