Expand description
Utilities for formatting and printing String
s.
This module contains the runtime support for the format!
syntax extension.
This macro is implemented in the compiler to emit calls to this module in
order to format arguments at runtime into strings.
§Usage
The format!
macro is intended to be familiar to those coming from C’s
printf
/fprintf
functions or Python’s str.format
function.
Some examples of the format!
extension are:
format!("Hello"); // => "Hello"
format!("Hello, {}!", "world"); // => "Hello, world!"
format!("The number is {}", 1); // => "The number is 1"
format!("{:?}", (3, 4)); // => "(3, 4)"
format!("{value}", value=4); // => "4"
let people = "Rustaceans";
format!("Hello {people}!"); // => "Hello Rustaceans!"
format!("{} {}", 1, 2); // => "1 2"
format!("{:04}", 42); // => "0042" with leading zeros
format!("{:#?}", (100, 200)); // => "(
// 100,
// 200,
// )"
From these, you can see that the first argument is a format string. It is required by the compiler for this to be a string literal; it cannot be a variable passed in (in order to perform validity checking). The compiler will then parse the format string and determine if the list of arguments provided is suitable to pass to this format string.