write
import std::fmt::write; · source
Types
FmtWrite
type trait FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}
The sink trait every formatter writes through. It parallels io::Write but with a different error type and a stricter contract.
write_str is the one required method; write_char has a default that encodes the scalar and routes through it. Its argument is a Str, so a sink only ever sees valid UTF-8 — that invariant is what lets a text container like String be a format target. A byte sink that isn't UTF-8-constrained is an io::Write, not an FmtWrite.
Methods
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_str(mut &this, source: Str) -> Result<(), FmtError>;Write source in full, returning when the whole view has been accepted by the sink or surfacing the first failure. This is the ONE required method; everything else routes through it. The argument is a Str, so the sink only ever sees valid UTF-8 - the invariant that lets a text container like String be a format target. (A byte sink that isn't UTF-8-constrained is an io::Write, not an FmtWrite.)
write_char(mut &this, c: char) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;Write a single Unicode scalar, UTF-8-encoded (1-4 bytes). Routes through write_str; the formatter uses it for punctuation such as the quotes Debug puts around a string.
Trait implementations
implement trait FmtWrite for struct Stdout
implement trait FmtWrite for struct Stderr
implement trait FmtWrite for struct String<GlobalAlloc>
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 FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}FmtWrite::write_str(mut &this, source: Str) -> Result<(), FmtError>Write source in full, returning when the whole view has been accepted by the sink or surfacing the first failure. This is the ONE required method; everything else routes through it.
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.
&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) source: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strprimitive ()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);
}FmtWrite::write_char(mut &this, c: char) -> Result<(), FmtError>Write a single Unicode scalar, UTF-8-encoded (1-4 bytes). Routes through write_str; the formatter uses it for punctuation such as the quotes Debug puts around a string.
(parameter) c: charimport 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).
FmtWrite::write_str(mut &this, source: Str) -> Result<(), FmtError>Write source in full, returning when the whole view has been accepted by the sink or surfacing the first failure. This is the ONE required method; everything else routes through it.
FmtWrite::write_char(mut &this, c: char) -> Result<(), FmtError>Write a single Unicode scalar, UTF-8-encoded (1-4 bytes). Routes through write_str; the formatter uses it for punctuation such as the quotes Debug puts around a string.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype trait Write {
write_some(mut &this, bytes: Slice<u8>) -> Result<u64, IoError>;
flush(mut &this) -> Result<(), IoError>;
write<T>(mut &this, data: T) -> Result<(), IoError>;
}type trait FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}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 referenceimplement 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 Stdout {}Handle to process standard output. Unbuffered; wrap in LineWriter<Stdout> or BufWriter<Stdout> from io::buf for the common "coalesce many small writes" case.
type struct Stderr {}Handle to process standard error. Unbuffered by convention so diagnostics appear immediately; do **not** wrap in BufWriter unless you also guarantee a flush on every path that might panic or exit.
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.