Skip to content

f"{p}" refuses a struct that implements Display, though print(p) uses that impl #3005

Closed

Description

@joshhickson
pub type Point { x: i32, y: i32 }

impl Display for Point:
    fn to_str() -> str: f"({self.x}, {self.y})"

fn main:
    let p = Point { x: 1, y: 2 }
    print(p)            // (1, 2): print[T: Display]
    print(f"{p:?}")     // Point { x: 1, y: 2 }: Debug (derived here; an explicit impl Debug is honored, D61)
    print(f"p = {p}")   // error: struct type has no default display; use :? for debug

The default form refuses every struct before looking for a Display impl (check_display_interpolant in
src/SemaCheck.w). That matches §15.4 ("Non-str expressions are converted to
str via built-in formatting functions (no trait dispatch)") and §15.4.8 ("Using {some_struct} without :? is a
compile-time error"), so this is a question about the rule, not a report that the compiler breaks it.

It matters for library types: print(f"loss = {loss}") with an array, a decimal, a date or a dataframe
is the first line many users write, and today the only spellings are print(x) alone, {x:?} (the
Debug form) or {x.to_str()}. :? already dispatches to a user impl, so {} dispatching to Display
would follow the same path. Format specs ({x:.2}) could stay float-only.

Question. Should {x} use impl Display when the type has one (and keep the current error when it
doesn't)?

Checked on Linux x86_64 (WSL2) with release builds of main 14a720bd, 86dba3e9, a719f858 and 365c2008; main is now ecbc23b5, and nothing since 365c2008 touches f-string display.

Activity

  1. added a commit that references this issue on Nov 7, 2026
  2. ehartford commented on Nov 7, 2026

    @ehartford
    Contributor

    Fixed in #3056 (merged as afbdbb1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions