Answer#
println!("{}", value) asks for the user-facing text of the value, through the Display trait. Numbers and strings have it. Your own structs do not, because there is no single right way to print them. Rust offers two formatting traits, and you choose per use:
{}usesDisplay: written by hand, meant for end users.{:?}usesDebug: can be generated with#[derive(Debug)], meant for developers.{:#?}prints it over several lines.
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
println!("{}", p);
}error[E0277]: `Point` doesn't implement `std::fmt::Display`
--> src/main.rs:8:20
|
8 | println!("{}", p);
| -- ^ `Point` cannot be formatted with the default formatter
| |
| required by this formatting parameter
|
help: the trait `std::fmt::Display` is not implemented for `Point`
--> src/main.rs:1:1
|
1 | struct Point {
| ^^^^^^^^^^^^
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.The answers below are the two fixes.