pub struct Template<'a> { /* private fields */ }Expand description
A parsed format string that can be rendered many times with different arguments.
§Examples
use formatx::Template;
let template = Template::new("{name} scored {score:.1}%").unwrap();
assert!(template.contains("name"));
let result = template.render()
.named("name", "Alice")
.named("score", &95.678)
.finish()
.unwrap();
assert_eq!(result, "Alice scored 95.7%");Implementations§
Source§impl<'a> Template<'a>
impl<'a> Template<'a>
Sourcepub fn new<S: AsRef<str> + ?Sized>(source: &'a S) -> Result<Self, Error>
pub fn new<S: AsRef<str> + ?Sized>(source: &'a S) -> Result<Self, Error>
Parse a format string into a reusable template.
Returns Err if the format string is malformed (unmatched braces, invalid specs, etc.).
Examples found in repository?
examples/struct.rs (line 32)
15fn main() {
16 let origin = Point { x: 0.0, y: 0.0 };
17 let target = Point { x: 3.5, y: -2.1 };
18
19 // Display
20 let result = formatx!("From {} to {}", origin, target).unwrap();
21 println!("{result}");
22
23 // Debug
24 let result = formatx!("Debug: {:?}", origin).unwrap();
25 println!("{result}");
26
27 // Pretty debug
28 let result = formatx!("Pretty: {:#?}", origin).unwrap();
29 println!("{result}");
30
31 // Template reuse
32 let template = Template::new("Point {name}: {point}").unwrap();
33 let r1 = template
34 .render()
35 .named("name", &"Origin")
36 .named("point", &origin)
37 .finish()
38 .unwrap();
39 let r2 = template
40 .render()
41 .named("name", &"Target")
42 .named("point", &target)
43 .finish()
44 .unwrap();
45 println!("{r1}");
46 println!("{r2}");
47}Sourcepub fn render(&self) -> Renderer<'_>
pub fn render(&self) -> Renderer<'_>
Create a Renderer to format this template with arguments.
The renderer collects arguments and produces the formatted output.
Examples found in repository?
examples/struct.rs (line 34)
15fn main() {
16 let origin = Point { x: 0.0, y: 0.0 };
17 let target = Point { x: 3.5, y: -2.1 };
18
19 // Display
20 let result = formatx!("From {} to {}", origin, target).unwrap();
21 println!("{result}");
22
23 // Debug
24 let result = formatx!("Debug: {:?}", origin).unwrap();
25 println!("{result}");
26
27 // Pretty debug
28 let result = formatx!("Pretty: {:#?}", origin).unwrap();
29 println!("{result}");
30
31 // Template reuse
32 let template = Template::new("Point {name}: {point}").unwrap();
33 let r1 = template
34 .render()
35 .named("name", &"Origin")
36 .named("point", &origin)
37 .finish()
38 .unwrap();
39 let r2 = template
40 .render()
41 .named("name", &"Target")
42 .named("point", &target)
43 .finish()
44 .unwrap();
45 println!("{r1}");
46 println!("{r2}");
47}Sourcepub fn contains(&self, name: &str) -> bool
pub fn contains(&self, name: &str) -> bool
Returns true if the template contains a placeholder with the given name.
Sourcepub fn placeholders(&self) -> Vec<&str>
pub fn placeholders(&self) -> Vec<&str>
Returns the names of all named placeholders in the template.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Template<'a>
impl<'a> RefUnwindSafe for Template<'a>
impl<'a> Send for Template<'a>
impl<'a> Sync for Template<'a>
impl<'a> Unpin for Template<'a>
impl<'a> UnsafeUnpin for Template<'a>
impl<'a> UnwindSafe for Template<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more