Why doesn't `Path` implement `std::fmt::Display`?

⚓ rust    📅 2025-06-18    👤 surdeus    👁️ 3      

surdeus

Why do we have to do this

fn main() {
    let path = std::path::Path::new("/tmp/foo.rs");
    println!("{}", path.display());
}

instead of this?

fn main() {
    let path = std::path::Path::new("/tmp/foo.rs");
    println!("{path}");
}

Is this this intentional?

Error message:

error[E0277]: `Path` doesn't implement `std::fmt::Display`
 --> src/main.rs:3:15
  |
3 |     println!("{path}");
  |               ^^^^^^ `Path` cannot be formatted with the default formatter; call `.display()` on it
  |
  = help: the trait `std::fmt::Display` is not implemented for `Path`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
  = 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)

9 posts - 5 participants

Read full topic

🏷️ rust_feed