pub struct Template { /* private fields */ }Expand description
An owned, 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 Template
impl Template
Sourcepub fn new<S: Into<String>>(source: S) -> Result<Self, Error>
pub fn new<S: Into<String>>(source: 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 = formatx::Template::new("Point {name}: {point}").unwrap();
33 let r1 = template.render()
34 .named("name", &"Origin")
35 .named("point", &origin)
36 .finish()
37 .unwrap();
38 let r2 = template.render()
39 .named("name", &"Target")
40 .named("point", &target)
41 .finish()
42 .unwrap();
43 println!("{r1}");
44 println!("{r2}");
45}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 33)
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 = formatx::Template::new("Point {name}: {point}").unwrap();
33 let r1 = template.render()
34 .named("name", &"Origin")
35 .named("point", &origin)
36 .finish()
37 .unwrap();
38 let r2 = template.render()
39 .named("name", &"Target")
40 .named("point", &target)
41 .finish()
42 .unwrap();
43 println!("{r1}");
44 println!("{r2}");
45}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 Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnsafeUnpin for Template
impl UnwindSafe for Template
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