Expand description
formatx lets you format strings at runtime using the same syntax as
std::fmt ({}, {:?}, {name}, etc.), but with runtime template
strings instead of compile-time literals with zero dependencies.
§Using formatx!
Works just like format!, but accepts runtime template strings.
use formatx::formatx;
let template = "{} scored {score:.1}% in {}";
let result = formatx!(template, "Alice", "maths", score = 95.678).unwrap();
assert_eq!(result, "Alice scored 95.7% in maths");Note: Extra arguments that aren’t referenced by any placeholder are
silently ignored in both formatx! and formatxl!.
§Template Reuse
Parse once, render many times with Template.
use formatx::Template;
let template = Template::new("{name} has {n} items").unwrap();
let r1 = template.render()
.named("name", &"Alice")
.named("n", &3)
.finish()
.unwrap();
let r2 = template.render()
.named("name", &"Bob")
.named("n", &7)
.finish()
.unwrap();
assert_eq!(r1, "Alice has 3 items");
assert_eq!(r2, "Bob has 7 items");Macros§
Structs§
- Renderer
- A builder for rendering a
Templatewith arguments. - Template
- An owned, parsed format string that can be rendered many times with different arguments.
Enums§
- Error
- Errors that can occur during parsing or formatting.
- Format
Type - The format trait to use.
Traits§
- Format
Value - Marker trait for values that can be formatted at runtime.