[−][src]Macro format_xml::template
Template strings.
Compiles down to a single format_args!
invocation.
Examples
Basic usage
let name = "World"; format_xml::template!("Hello "{name}"!").to_string()
The resulting string is Hello World!
.
Note how the expression values to be formatted are inlined in the formatting braces. Due to limitations of macros by example the formatting braces are not part of the surrounding string literals.
Formatting specifiers
let value = 42; format_xml::template!("hex("{value}") = "{value;#x}).to_string()
The resulting string is hex(42) = 0x2a
.
Due to limitations of macros by example, a semicolon is used to separate the value from the formatting specifiers. The rules for the specifiers are exactly the same as Rust's standard formatting syntax.
Control flow
let switch = true; let opt = Some("World"); let result: Result<f32, i32> = Err(13); format_xml::template! { if let Some(name) = (opt) { "# Hello " {name}"\n\n" } else if (switch) { "# Hello User\n\n" } if (switch) { "Message: " match (result) { Ok(f) => { "_"{f}"_\n\n" } Err(i) => { "**"{i}"**\n\n" } } for i in (1..=5) { let times_five = i * 5; "* "{i}" x 5 = "{times_five}"\n" } } else { "No contents" } }.to_string()
The resulting string is # Hello World\n\nMessage: **13**\n\n* 1 x 5 = 5\n* 2 x 5 = 10\n* 3 x 5 = 15\n* 4 x 5 = 20\n* 5 x 5 = 25\n
.
Custom formatting
format_xml::template! { |f| { f.write_str("custom formatting") }}.to_string()
If all else fails the closure syntax enables to write custom formatting code.
The signature is the same as the fmt
method of Rust's standard formatting traits.
f
is std::fmt::Formatter
and the closure returns a std::fmt::Result
.