pub trait StrExt {
// Required method
fn fill_into<F, W, E>(
&self,
output: &mut W,
filler: F,
) -> Result<(), Error<'_, E>>
where F: Filler<W, E>,
W: Write;
// Provided method
fn fill_to_string<F, E>(&self, filler: F) -> Result<String, Error<'_, E>>
where F: Filler<String, E> { ... }
}Expand description
String extension methods for the template string.
This is generally how I recommend using this library, because I find that the method receiver
makes code clearer: that template.fill_into(output, filler) is easier to understand than
fill(template, filler, output).
Required Methods§
Sourcefn fill_into<F, W, E>(
&self,
output: &mut W,
filler: F,
) -> Result<(), Error<'_, E>>
fn fill_into<F, W, E>( &self, output: &mut W, filler: F, ) -> Result<(), Error<'_, E>>
Fill this template string into the provided string, with the provided filler.
Uses an existing string, which is more efficient if you want to push to an existing string or can reuse a string allocation.
Example, using a closure:
let filler = |output: &mut String, key: &str| {
match key {
"name" => output.push_str("world"),
_ => return Err(()),
}
Ok(())
};
let mut string = String::new();
assert!("Hello, {name}!".fill_into(&mut string, filler).is_ok());
assert_eq!(string, "Hello, world!");Provided Methods§
Sourcefn fill_to_string<F, E>(&self, filler: F) -> Result<String, Error<'_, E>>
Available on crate feature alloc only.
fn fill_to_string<F, E>(&self, filler: F) -> Result<String, Error<'_, E>>
alloc only.Fill this template, producing a new string.
This is a convenience method for ergonomics in the case where you aren’t fussed about
allocations and are using the standard String type.
Example, using a hash map:
let map = [("name", "world")].into_iter().collect::<HashMap<_, _>>();
assert_eq!(
"Hello, {name}!".fill_to_string(&map).unwrap(),
"Hello, world!",
);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.