docs

v0.1.0 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

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 FmtError

A failed write. Writing to memory never fails.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for FmtError
  • impl Debug for FmtError
  • impl Error for FmtError

FormatSpecSource

pub struct FormatSpec

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

  • pub const fn fill(self) -> char§

    Source

    The fill character (' ' when none is given).

  • pub const fn align(self) -> Alignment | none§

    Source

    none when the spec names no alignment.

  • pub const fn sign(self) -> Sign | none§

    Source

    + or -; none when the spec names no sign.

  • pub const fn alternate(self) -> bool§

    Source

    #.

  • pub const fn zero_pad(self) -> bool§

    Source

    0.

  • pub const fn width(self) -> usize | none§

    Source

    The minimum width; none when not given.

  • pub const fn precision(self) -> usize | none§

    Source

    The precision; none when not given.

  • pub const fn ty(self) -> char§

    Source

    The type letter ('\0' for none or s d).

  • pub const fn new() -> FormatSpec§

    Source

    The empty spec (FormatSpec::default() too).

  • pub const fn parse(text: str) throws (ValueError) -> FormatSpec§

    Source

    text parsed by the runtime with the parser the compiler checks static specs with; throws on a malformed spec.

Trait implementations 1

FormatterSource

pub struct Formatter

Where 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).

  • pub const fn new() -> Formatter§

    Source

    An empty formatter with the plain spec, room for 16 bytes.

  • pub const fn with_capacity(cap: usize) -> Formatter§

    Source

    Empty, with room for cap bytes.

  • pub fn from_sink(sink: fn(str) -> bool) -> Formatter§

    Source

    Empty, handing its text to sink in chunks as it is written, so the whole text is never held at once. sink returns false when it cannot take a chunk; the write then fails with FmtError. Call flush after the last write.

  • pub const fn flush(ref self) throws (FmtError)§

    Source

    Hand what a from_sink formatter holds to its sink; nothing for any other.

  • pub const fn with_spec(spec: FormatSpec) -> Formatter§

    Source

    Empty, writing values per spec.

  • pub const fn finish(self) -> str§

    Source

    The text written, as a str (no copy); the formatter is spent.

  • pub const fn write_str(ref self, s: str) throws (FmtError)§

    Source

    Append s; throws FmtError when a from_sink formatter's sink refuses a chunk.

  • pub const fn write_char(ref self, c: char) throws (FmtError)§

    Source

    Append c; never throws.

  • pub const fn pad(ref self, s: str) throws (FmtError)§

    Source

    s cut to precision chars and padded to width (text aligns left by default).

  • pub const fn pad_integral(ref self, is_nonnegative: bool, prefix: str, buf: str) throws (FmtError)§

    Source

    An integer: the sign, prefix ("0x" ..) under #, the digits buf, padded to width - right by default; 0 and = fill between the prefix and the digits.

  • pub const fn fill(self) -> char§

    Source

    The spec's fill character (' ' when none is given).

  • pub const fn align(self) -> Alignment | none§

    Source

    none when the spec names no alignment: numbers then align right, other values left.

  • pub const fn sign(self) -> Sign | none§

    Source

    The spec's sign (+ or -); none when it names none.

  • pub const fn alternate(self) -> bool§

    Source

    The spec's # flag.

  • pub const fn zero_pad(self) -> bool§

    Source

    The spec's 0 flag.

  • pub const fn width(self) -> usize | none§

    Source

    The spec's minimum width; none when not given.

  • pub const fn precision(self) -> usize | none§

    Source

    The spec's precision; none when not given.

  • pub const fn ty(self) -> char§

    Source

    The spec's type letter ('\0' for none or s d).

  • pub const fn write_fmt(ref self, args: Arguments) throws (FmtError)§

    Source

    Write args (an f-string) piece by piece, with no str of the whole text: try f.write_fmt(f"({self.x}, {self.y})").

Trait implementations 1

ArgumentsSource

pub struct Arguments

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

Trait implementations 2

Enums

AlignmentSource

pub enum Alignment

The spec's align.

SignSource

pub enum Sign

The spec's sign.

Traits

RichDisplaySource

pub trait RichDisplay

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

nabi
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
  • fn mime_bundle(self) -> Dict<str, str>§

    Source

    MIME type to content: image/svg+xml, text/html, text/markdown, text/latex and application/json as text, image/png and other binary types base64-encoded, text/plain for front ends that show none of the others.

DisplaySource

pub trait Display

What f"{x}" writes.

ToStrSource

pub trait ToStr

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

  • fn to_str(self) -> str§

    Source

    The Display text of self.

DebugSource

pub trait Debug

What f"{x:?}" writes.