Skip to content
CryoCryo home
Stdlibfmt

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 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 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>