Skip to content

std/display

Import with import std/display.

Interfaces

interface Display {
    fn to_string(value: self): String
}

Protocol for converting a value to a human-readable String.

Display is the canonical “stringify” interface. The runtime stringifier (used by IO.print and string interpolation "${value}") produces the same form an impl of Display must reproduce, so user types interoperate with the built-in path uniformly. Typed-literal handler types require the per-slot value-type interface to extend Display so every interpolated value has a canonical rendering.

All four primitive types — Int, Float, Bool, String — implement Display. To implement it for a custom type:

pub struct Greeter {
  name: String
}

impl Display for Greeter {
  fn to_string(g: Greeter): String { "hi ${g.name}" }
}