error[E0277]: type doesn’t implement std::fmt::Display
A type is missing a trait that the code needs. With println!("{}") that trait is Display; in general, E0277 means a trait bound isn’t satisfied.
Quick fix: For printing, add #[derive(Debug)] and use {:?}, or implement Display. In general, implement or derive the trait named in the message.
Code that triggers E0277
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("{p}");
println!("x + y = {}", p.x + p.y);
}What the compiler says
error[E0277]: `Point` doesn't implement `std::fmt::Display`
--> src/main.rs:8:15
|
8 | println!("{p}");
| ^^^ `Point` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `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`.Why Rust raises E0277
println!("{p}") uses the {} placeholder, which requires std::fmt::Display. Point doesn’t implement it, and Rust won’t guess how your type should look to users, so there’s no automatic Display.
The same error code appears whenever a trait bound isn’t satisfied: passing a type to a function that needs Clone, sorting values that aren’t Ord, using a type as a HashMap key without Hash, and so on. The help: line always names the missing trait.
One E0277 surprises almost everyone: using ? inside main.
? returns early with the error, which only works in a function that returns Result or Option:
use std::fs;
fn main() {
let text = fs::read_to_string("notes.txt")?;
println!("{text}");
}error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:4:47
|
3 | fn main() {
| --------- this function should return `Result` or `Option` to accept `?`
4 | let text = fs::read_to_string("notes.txt")?;
| ^ cannot use the `?` operator in a function that returns `()`
|
help: consider adding return type
|
3 ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
4 | let text = fs::read_to_string("notes.txt")?;
5 | println!("{text}");
6 + Ok(())
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.How to fix E0277
1. Derive Debug and print with {:?}
For developer output, derive Debug and use {:?}, or {:#?} for pretty-printing. This is what the compiler’s note suggests.
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("{p:?}");
println!("x + y = {}", p.x + p.y);
}2. Implement Display yourself
For output meant for users, implement Display and decide the format. Then {} works, and so does .to_string().
use std::fmt;
struct Point {
x: i32,
y: i32,
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("{p}");
println!("x + y = {}", p.x + p.y);
}3. Make the function return Result to use ?
For the ? version, give the function a Result return type and end with Ok(()). Box<dyn std::error::Error> accepts any error type, which is handy in main.
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let text = fs::read_to_string("notes.txt")?;
println!("{text}");
Ok(())
}Related errors
- E0369: binary operation
==cannot be applied to type - E0599: no method named
push_strfound for reference&str - E0308: mismatched types
Learn the concept behind it
The Structs by Example lesson explains the rules behind E0277 with diagrams and more examples. For the official reference, run rustc --explain E0277 or read the E0277 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.