Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
struct P {
x: i32,
}
fn main() {
let p = P { x: 1 };
println!("{} {:?}", p.x, p);
}It does not compile
- Expecting
{:?}to print any struct, asconsole.logdoes in JavaScript: a struct getsDebugonly from#[derive(Debug)]or a manual impl, so here it is error E0277. - Expecting a panic at run time: the trait behind each placeholder is checked while building, so the program never runs.
- What does the program print once
#[derive(Debug)]is added abovestruct P? - Why does
{}withpitself still fail after#[derive(Debug)]is added?