macro_rules! format {
($max:expr; $lenT:path; $($arg:tt)*) => { ... };
($max:expr; $($arg:tt)*) => { ... };
($($arg:tt)*) => { ... };
}
Expand description
Macro that creates a fixed capacity String
. Equivalent to format!
.
The macro’s arguments work in the same way as the regular macro.
It is possible to explicitly specify the capacity of the returned string as the first argument. In this case it is necessary to disambiguate by separating the capacity with a semicolon.
§Errors
There are two possible error cases. Both return the unit type core::fmt::Error
.
- In case the formatting exceeds the string’s capacity. This error does not exist in the standard library as the string would just grow.
- If a formatting trait implementation returns an error. The standard library panics in this case.
§Examples
use heapless::{format, String};
// Notice semicolon instead of comma!
format!(4; "test")?;
format!(15; "hello {}", "world!")?;
format!(20; "x = {}, y = {y}", 10, y = 30)?;
let (x, y) = (1, 2);
format!(12; "{x} + {y} = 3")?;
let implicit: String<10> = format!("speed = {}", 7)?;