display
import std::fmt::display; · source
Display
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}
Human-readable output for end users. Implement one for your own type and it becomes usable in every f-string hole, every print, and every format_to_string call:
implement trait Display for struct Point {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite {
f.write_str(Str::new("("))?;
this.x.fmt(f)?;
f.write_str(Str::new(", "))?;
this.y.fmt(f)?;
return f.write_str(Str::new(")"));
}
}
Debug
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}
Developer-readable output for diagnostics, with the identical shape. Debug is the unambiguous form. For Str and String it wraps the content in double quotes and escapes ", \, control bytes, and non-ASCII low codepoints, so the output is valid UTF-8 whatever the source and round-trips through a parser that understands \n, \r, \t, \\, \", and \xNN. For char it uses single quotes and emits non-ASCII scalars as \u{XXXX} to keep terminal output safe.
Display for char encodes the scalar as UTF-8 rather than emitting the low byte, which would truncate non-ASCII to garbage. Surrogates are not legal Unicode and produce U+FFFD.
Formatting to a String
function format_to_string<T>(value: &T) -> Result<String, FmtError>
where T: Display;
function format_debug_to_string<T>(value: &T) -> Result<String, FmtError>
where T: Debug;
match (format_to_string(&value)) {
Result::Ok(s) => { /* you own `s` */ }
Result::Err(e) => { /* FmtError */ }
}
format_debug_to_string is the Debug counterpart. You own the result and are responsible for dropping it.
Implementors
Both traits ship for every primitive, Str, String, Option, Result, the collections, Pair, and every standard library error type. The impls for the core and collections types live in this file rather than beside each type, because fmt sits above them in the layering and the lower modules cannot import it; the ones marked with another module — Duration, DateTime, TestError — are declared where their type is.
implement trait Display for u8
implement trait Display for u16
implement trait Display for u32
implement trait Display for u64
implement trait Display for u128
implement trait Display for i8
implement trait Display for i16
implement trait Display for i32
implement trait Display for i64
implement trait Display for i128
implement trait Display for usize
implement trait Display for isize
implement trait Display for f32
implement trait Display for f64
implement trait Display for boolean
implement trait Display for string
implement trait Display for char
implement trait Display for struct Str
implement<A> trait Display for struct String<A>
where A: Allocator
implement<T> trait Display for enum Option<T>
where T: Display
implement<T, E> trait Display for enum Result<T, E>
where T: Display, E: Display
implement<T, A> trait Display for struct Array<T, A>
where T: Display, A: Allocator
implement<K, V, A> trait Display for struct HashMap<K, V, A>
where K: Display, V: Display, A: Allocator
implement<T, A> trait Display for struct HashSet<T, A>
where T: Display, A: Allocator
implement<A, B> trait Display for struct Pair<A, B>
where A: Display, B: Display
implement trait Display for struct IoError
implement trait Display for struct JsonError
implement trait Display for enum RandomError
implement trait Display for struct AllocError
implement trait Display for struct ConversionError
implement trait Display for enum FmtError
implement trait Display for enum TestError // std::test::error
implement trait Display for struct DateTime // std::time::datetime
implement trait Display for struct Duration // std::time::duration
implement trait Debug for u8
implement trait Debug for u16
implement trait Debug for u32
implement trait Debug for u64
implement trait Debug for u128
implement trait Debug for i8
implement trait Debug for i16
implement trait Debug for i32
implement trait Debug for i64
implement trait Debug for i128
implement trait Debug for usize
implement trait Debug for isize
implement trait Debug for f32
implement trait Debug for f64
implement trait Debug for boolean
implement trait Debug for char
implement trait Debug for struct Str
implement trait Debug for struct String<GlobalAlloc>
implement<T> trait Debug for enum Option<T>
where T: Debug
implement<T, E> trait Debug for enum Result<T, E>
where T: Debug, E: Debug
implement<T, A> trait Debug for struct Array<T, A>
where T: Debug, A: Allocator
implement<K, V, A> trait Debug for struct HashMap<K, V, A>
where K: Debug, V: Debug, A: Allocator
implement<T, A> trait Debug for struct HashSet<T, A>
where T: Debug, A: Allocator
implement<A, B> trait Debug for struct Pair<A, B>
where A: Debug, B: Debug
implement trait Debug for struct IoError
implement trait Debug for struct JsonError
implement trait Debug for enum RandomError
implement trait Debug for struct AllocError
implement trait Debug for struct ConversionError
implement trait Debug for enum FmtError
implement trait Debug for struct Duration // std::time::duration
Formatter<W>
type struct Formatter<W> {
writer: W*;
static new(writer: W*) -> Formatter<W>
where W: FmtWrite;
write_str(mut &this, source: Str) -> Result<(), FmtError>
where W: FmtWrite;
write_char(mut &this, c: char) -> Result<(), FmtError>
where W: FmtWrite;
get_writer(&this) -> W*;
}
Formatter<W> borrows its writer through a raw pointer — the caller owns the sink and is responsible for dropping it, which is what lets a Formatter<String> hand the string straight back without into_inner gymnastics. get_writer is for dispatching a second formatter against the same sink or flushing mid-stream.