View source code
Display the source code in std/format.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.format.format

Format arguments into a string.

typeof(fmt) format(alias fmt, Args...) (
  Args args
)
if (isSomeString!(typeof(fmt)));

immutable(Char)[] format(Char, Args...) (
  in Char[] fmt,
  Args args
)
if (isSomeChar!Char);

If the format string is fixed, passing it as a template parameter checks the type correctness of the parameters at compile-time. This also can result in better performance.

Parameters

NameDescription
fmt Format string. For detailed specification, see formattedWrite.
args Variadic list of arguments to format into returned string.

Throws

if the number of arguments doesn't match the number of format parameters and vice-versa.

Example

Type checking can be done when fmt is known at compile-time:

auto s = format!"%s is %s"("Pi", 3.14);
writeln(s); // "Pi is 3.14"

static assert(!__traits(compiles, {s = format!"%l"();}));     // missing arg
static assert(!__traits(compiles, {s = format!""(404);}));    // surplus arg
static assert(!__traits(compiles, {s = format!"%d"(4.03);})); // incompatible arg

Authors

Walter Bright, Andrei Alexandrescu, and Kenji Hara

License

Boost License 1.0.