# \#\[derive(Display("({x}, {y})"))\]

**URL:** <https://internals.rust-lang.org/t/derive-display-x-y/23026>\
**Category:** libs\
**Created:** [June 1, 2025, 8:09pm UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026 "2025-06-01T20:09:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [June 1, 2025, 8:09pm UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/1 "2025-06-01T20:09:20Z")

</div>

While it is nice to get `Debug` derived, sadly the same is not true for the similar `Display`. Now obviously there is no canonical right implementation, but often only the inner part of five lines varies. So I propose this:

```rust
#[derive(Debug, Display("({}, {})", self.x, self.y))]
struct Point {
    x: i32,
    y: i32,
}

```

which would generate this:

```rust
impl Display for Point {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

```

In case of a struct one could go even further, aliasing the members:

```rust
#[derive(Display("({x}, {y})"))]

```

While a tuple struct could maybe have a less pretty:

```rust
#[derive(Display("({_0}, {_1})"))]

```

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [June 1, 2025, 8:14pm UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/2 "2025-06-01T20:14:22Z")

</div>

It would be really nice if derives could receive parameters. For comparison, the equivalent [derive\_more](https://docs.rs/derive_more/latest/derive_more/derive.Display.html) code takes an extra line

```rust
#[derive(Debug, Display)]
#[display("({x}, {y})")]
struct Point {
    x: i32,
    y: i32,
}

```

---

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [June 2, 2025, 2:10am UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/3 "2025-06-02T02:10:56Z")

</div>

How is this supposed to handle padding, alignment, and precision?

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [June 2, 2025, 2:33am UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/4 "2025-06-02T02:33:16Z")

</div>

Maybe using the same syntax from `format!()`? Like the `derive_more` crate does

---

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [June 2, 2025, 9:49am UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/5 "2025-06-02T09:49:38Z")

</div>

> [@dlight](#):
>
> using the same syntax from `format!()`

@user16251 Certainly, that’s pretty much the Rust standard! This is the syntax of `format_args!()`, which is the common denominator of `print!()`, `write!()`, `log::warn!()`, etc. My title was meant to imply that you should get all that goodness.

---

<div class="post-metadata">

**Author:** ![danjl1100](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danjl1100/32/6466_2.png) [@danjl1100](https://internals.rust-lang.org/u/danjl1100)\
**Post date:** [June 2, 2025, 12:23pm UTC](https://internals.rust-lang.org/t/derive-display-x-y/23026/6 "2025-06-02T12:23:10Z")

</div>

> [@dlight](#):
>
> the equivalent [derive\_more](https://docs.rs/derive_more/latest/derive_more/derive.Display.html) code takes an extra line
> 
> ```rust
> #[derive(Debug, Display)]
> #[display("({x}, {y})")]
> struct Point {
> x: i32,
> y: i32,
> }
> 
> ```

Another benefit of the extra line is to be able to use this on enums too, like `derive_more` does (`#[display(...)]` [on each variant](https://docs.rs/derive_more/latest/derive_more/derive.Display.html#wrapping-enum-format))
