display
import std::fmt::display; · source
Types
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(")"));
}
}
Methods
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError> where W: FmtWrite;
Implementors
Display ships 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 rest — Duration, DateTime, TestError — are declared where their type is, and name their module.
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
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.
Methods
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError> where W: FmtWrite;
Implementors
Debug ships beside Display for the primitives, the core and collections types, and the error types, from this file for the same layering reason; Duration's is declared in its own module.
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.
Properties
| Property | Type |
|---|---|
writer | W* |
Constructors
static new(writer: W*) -> Formatter<W> where W: FmtWrite;
static new(writer: W*) -> Formatter<W> where W: FmtWrite;Construct over any FmtWrite sink. The bound is restated on each instance method below; Cryo's monomorphizer doesn't carry where-clauses from a static factory into the surrounding impl block, so methods that route through W must declare the bound themselves.
Methods
write_str(mut &this, source: Str) -> Result<(), FmtError> where W: FmtWrite;
write_str(mut &this, source: Str) -> Result<(), FmtError> where W: FmtWrite;Write a Str. The usual path for fixed text and for pre-rendered ASCII (integer digits, escapes) wrapped as a Str.
write_char(mut &this, c: char) -> Result<(), FmtError> where W: FmtWrite;
write_char(mut &this, c: char) -> Result<(), FmtError> where W: FmtWrite;Write a single Unicode scalar, UTF-8-encoded.
get_writer(&this) -> W*;
get_writer(&this) -> W*;Surface the underlying writer. For advanced composition where the caller wants to dispatch a second formatter against the same sink, or to flush mid-stream.
Free Functions
Formatting to a String
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.
function format_to_string<T>(value: &T) -> Result<String, FmtError> where T: Display;
function format_to_string<T>(value: &T) -> Result<String, FmtError> where T: Display;Format value into a fresh heap-backed String. Caller owns the result and is responsible for dropping it.
function format_debug_to_string<T>(value: &T) -> Result<String, FmtError> where T: Debug;
function format_debug_to_string<T>(value: &T) -> Result<String, FmtError> where T: Debug;Same shape for Debug.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
Display::fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWritetype parameter Wwhere W: FmtWrite
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) f: mut &Formatter<W>mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
type struct Formatter<W> {
writer: W*;
}primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type enum FmtError {
Io(IoError);
Alloc(AllocError);
}where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displayimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct PointPoint::fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite(parameter) f: mut &Formatter<W>Formatter::write_str(mut &this, source: Str) -> Result<(), FmtError>
where W: FmtWriteWrite a Str. The usual path for fixed text and for pre-rendered ASCII (integer digits, escapes) wrapped as a Str.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. The enclosing function must return a matching Result / Option.
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Debug::fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWritetype struct Formatter<W> {
writer: W*;
}type parameter WFormatter.writer: W*type parameter Wstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Formatter::new(writer: W*) -> Formatter<W>
where W: FmtWriteConstruct over any FmtWrite sink. The bound is restated on each instance method below; Cryo's monomorphizer doesn't carry where-clauses from a static factory into the surrounding impl block, so methods that route through W must declare the bound themselves.
(parameter) writer: W*Formatter::write_str(mut &this, source: Str) -> Result<(), FmtError>
where W: FmtWriteWrite a Str. The usual path for fixed text and for pre-rendered ASCII (integer digits, escapes) wrapped as a Str.
(parameter) source: StrFormatter::write_char(mut &this, c: char) -> Result<(), FmtError>
where W: FmtWriteWrite a single Unicode scalar, UTF-8-encoded.
(parameter) c: charFormatter::get_writer(&this) -> W*Surface the underlying writer. For advanced composition where the caller wants to dispatch a second formatter against the same sink, or to flush mid-stream.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
function format_to_string<T>(value: &T) -> Result<String, FmtError>
where T: DisplayFormat value into a fresh heap-backed String. Caller owns the result and is responsible for dropping it.
Result::Ok(T)s: Stringpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: FmtErrorimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Display::fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWritetype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairtype struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
type struct DateTime {
year: i64;
month: u32;
day: u32;
hour: u32;
minute: u32;
second: u32;
}A UTC civil calendar reading. All fields are UTC and already normalized: month in 1..=12, day in 1..=31, hour in 0..=23, minute/second in 0..=59. year is the proleptic Gregorian year and may be negative for timestamps far before the Common Era. POD.
type enum TestError {
Failed(String);
Other(String);
}Why a test failed. The runner prints the variant's message verbatim to stderr from the child process before exiting non-zero, so the parent can show the failing message in the test summary.
std::test::errorprimitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typeprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
type parameter Awhere A: Allocator
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatortype parameter Twhere T: Display
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type parameter Ewhere E: Display
type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype parameter Kwhere K: Display
type parameter Vwhere V: Display
type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmaptype struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
std::collections::hashsettype parameter Awhere A: Display
type parameter Bwhere B: Display
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}type enum RandomError {
EntropyUnavailable;
EmptyRange;
InvalidWeights;
}What went wrong in a fallible random operation.
std::random::errortype struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}type struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}for (mut i: i32 = 0; i < n; i++) { ... }
for (item in iterable) { ... }Two forms. The C-style for (init; condition; step) scopes its loop variable to the body. for (x in expr) iterates anything with next() -> Option<T>: an iterator, a range such as 0..n, an Array or Slice (their iter() is inserted for you), or a fixed-size array.
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
type parameter Twhere T: Debug
type parameter Ewhere E: Debug
type parameter Kwhere K: Debug
type parameter Vwhere V: Debug
type parameter Awhere A: Debug
type parameter Bwhere B: Debug
Formatter::get_writer(&this) -> W*Surface the underlying writer. For advanced composition where the caller wants to dispatch a second formatter against the same sink, or to flush mid-stream.
std::fmt::displayFormatter.writer: W*static Formatter::new(writer: W*) -> Formatter<W>
where W: FmtWriteConstruct over any FmtWrite sink. The bound is restated on each instance method below; Cryo's monomorphizer doesn't carry where-clauses from a static factory into the surrounding impl block, so methods that route through W must declare the bound themselves.
Formatter::write_char(mut &this, c: char) -> Result<(), FmtError>
where W: FmtWriteWrite a single Unicode scalar, UTF-8-encoded.
std::fmt::displayfunction format_debug_to_string<T>(value: &T) -> Result<String, FmtError>
where T: DebugSame shape for Debug.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function format_to_string<T>(value: &T) -> Result<String, FmtError>
where T: DisplayFormat value into a fresh heap-backed String. Caller owns the result and is responsible for dropping it.
(parameter) value: &Tfunction format_debug_to_string<T>(value: &T) -> Result<String, FmtError>
where T: DebugSame shape for Debug.