How to skip fields in a Display implementation?

⚓ rust    📅 2025-06-14    👤 surdeus    👁️ 1      

surdeus

I wish to skip certain fields when using the Display format to print it. As a test, I tried the main.rs program below:

// fmt
use std::fmt;

// Point2D
#[derive(Debug)]
struct Point2D {
    x: f64,
    y: f64,
}

// Display for Point2D.
impl fmt::Display for Point2D {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Customize so only x is displayed
        write!(f, "x: {}", self.x)
    }
}

// main
fn main() {
    let point = Point2D { x: 3.3, y: 7.2 };
    println!("Display: {}", point);
    println!("Debug: {:?}", point);
}

The output after Display below is what I am trying to get. Even though y is used by the Debug print, the program above gives a warning about y not being used. How should one display a subset of the fields in a structure ?

   Compiling cargo_test v0.1.0 (/Users/bradbell/trash/rust/cargo_test)
warning: field `y` is never read
 --> src/main.rs:8:5
  |
6 | struct Point2D {
  |        ------- field in this struct
7 |     x: f64,
8 |     y: f64,
  |     ^
  |
  = note: `Point2D` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: `cargo_test` (bin "cargo_test") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.55s
     Running `target/debug/cargo_test`
Display: x: 3.3
Debug: Point2D { x: 3.3, y: 7.2 }

4 posts - 4 participants

Read full topic

🏷️ rust_feed