Function in string compiler

format

fn format(string fmt) -> string
Import
use string::{format};
Source: compiler/src/builtin_docs/string.hy

Builds a string from the literal fmt and the arguments that follow it.

format takes any number of arguments after fmt; each % specifier in fmt consumes the next one:

  • %i: an int (or byte) in decimal.
  • %f: a float (1.5, 2.0).
  • %s: a string.
  • %z: a bool (true / false).
  • %v: any value with a Show instance (enums, tuples, records, …).
  • %x, %b, %u: an int in hex, binary, or unsigned decimal.
  • %%: a literal %.

fmt must be a string literal: the compiler checks every specifier against its argument’s type, and reports too many or too few arguments. format is built into the compiler, not a library function.

Parameters#

NameType
fmtstring

Returns#

string

Examples#

use string::{format};
use tests::assert;
assert!(format("%i-%s", 42, "x") == "42-x");
assert!(format("%f %z", 1.5, true) == "1.5 true");
assert!(format("%x %b %%", 255, 5) == "ff 101 %");
assert!(format("%v", (1, "a")) == "(1, a)");

See also#

↑ ↓ navigate ↵ open