Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
#[derive(Debug)]
struct Meters(i32);
fn main() {
let m = Meters(5);
println!("{}", m);
}It does not compile
- Expecting
Meters(5), as if{}fell back toDebug:#[derive(Debug)]serves only{:?}, andDisplayis never derived, so{}here is error E0277. - Expecting
5, as if a one-field tuple struct printed as its value:Metersis a separate type, and it has noDisplayuntil you implement it by hand.
- How would you implement
Displayso that{}prints5 m? - Why does the standard library refuse to derive
Displayfor you?