core::fmt
本頁內容
API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。
Formatter, Display / Debug, FormatSpec and the
f-string lowering targets.
Every rendering writes into a Formatter: the runtime's growing text
plus the spec of the value being written. HIR lowers f"..." to one
such text: literal pieces and primitive holes go to the runtime
directly, any other hole through Display::fmt / Debug::fmt
(fstr_* below), and nabi_fmt_finish hands the text over as the
str without a copy. A formatter made by from_sink instead hands its
text out in chunks as it grows (io::Write::write_display).
Structs
FmtErrorSource
pub struct FmtErrorA failed write. Writing to memory never fails.
What went wrong, for display.
FormatSpecSource
pub struct FormatSpecAn f-string spec, [[fill]align][sign][#][0][width][.precision][type].
An absent ty is '\0'; the Display letters
s d read as absent. Build one with parse, read it with the
getters.
The fill character (
' 'when none is given).nonewhen the spec names no alignment.+or-;nonewhen the spec names no sign.#.0.The minimum width;
nonewhen not given.The precision;
nonewhen not given.The type letter (
'\0'for none ors d).The empty spec (
FormatSpec::default()too).
Sourcepub const fn parse(§text: str) throws (ValueError) -> FormatSpec textparsed by the runtime with the parser the compiler checks static specs with; throws on a malformed spec.
Trait implementations 1
impl Default for FormatSpec
FormatterSource
pub struct FormatterWhere a value's text goes, with the spec it is written by:
the runtime's growing text (nabi_fmt_*) and the spec packed in two
words - flags: fill (bits 0..32), align, sign, type (a byte each
from bit 32), # (bit 56), 0 (bit 57); counts: width, precision
(32 bits each, all ones when absent).
An empty formatter with the plain spec, room for 16 bytes.
Empty, with room for
capbytes.Empty, handing its text to
sinkin chunks as it is written, so the whole text is never held at once.sinkreturnsfalsewhen it cannot take a chunk; the write then fails withFmtError. Callflushafter the last write.Hand what a
from_sinkformatter holds to its sink; nothing for any other.
Sourcepub const fn with_spec(§spec: FormatSpec) -> Formatter Empty, writing values per
spec.The text written, as a
str(no copy); the formatter is spent.Append
s; throwsFmtErrorwhen afrom_sinkformatter's sink refuses a chunk.Append
c; never throws.scut toprecisionchars and padded towidth(text aligns left by default).
Sourcepub const fn pad_integral(§ref self, is_nonnegative: bool, prefix: str, buf: str) throws (FmtError) An integer: the sign,
prefix("0x"..) under#, the digitsbuf, padded towidth- right by default;0and=fill between the prefix and the digits.The spec's fill character (
' 'when none is given).nonewhen the spec names no alignment: numbers then align right, other values left.The spec's sign (
+or-);nonewhen it names none.The spec's
#flag.The spec's
0flag.The spec's minimum width;
nonewhen not given.The spec's precision;
nonewhen not given.The spec's type letter (
'\0'for none ors d).Write
args(an f-string) piece by piece, with nostrof the whole text:try f.write_fmt(f"({self.x}, {self.y})").
Trait implementations 1
impl Debug for Formatter
ArgumentsSource
pub struct ArgumentsAn f-string written where an Arguments is expected (a parameter
args: fmt::Arguments): its holes are evaluated where it stands,
in order, but its text is made only when it is formatted, piece by
piece into the formatter, never as one str. Display writes it.
Enums
AlignmentSource
pub enum AlignmentThe spec's align.
SignSource
pub enum SignThe spec's sign.
Traits
RichDisplaySource
pub trait RichDisplayRenderings of a value a notebook front end shows (an image, a table,
a formula): nabi jupyter sends them for a cell whose value has
them, and display(x) sends them mid-cell. The terminal REPL and a
program use text/plain only.
struct Dot {}
impl RichDisplay for Dot {
fn mime_bundle(self) -> Dict<str, str> {
let b: Dict<str, str> = Dict::new();
b["image/svg+xml"] = "<svg xmlns=\"http://www.w3.org/2000/svg\"/>";
b["text/plain"] = "a dot";
b
}
}
display(Dot {}); // prints `a dot` outside a notebook
MIME type to content:
image/svg+xml,text/html,text/markdown,text/latexandapplication/jsonas text,image/pngand other binary types base64-encoded,text/plainfor front ends that show none of the others.
DisplaySource
pub trait DisplayWhat f"{x}" writes.
Writes
selffor display tof; throwsFmtErrorwhenffails.
ToStrSource
pub trait ToStrx.to_str(): a Display value's text, as f"{x}". Python:
str(x). Every Display type has it; a type's own to_str wins.
The
Displaytext ofself.
DebugSource
pub trait DebugWhat f"{x:?}" writes.
Writes
selffor debugging tof; throwsFmtErrorwhenffails.