Function interpolator::write

source ·
pub fn write<'a, K: Borrow<str> + Eq + Hash, F: Borrow<Formattable<'a>>>(
    out: &mut impl Write,
    format: &str,
    context: &'a HashMap<K, F>
) -> Result<(), Error>
Expand description

Runtime version of write!.

Takes a mutable Write e.g. &mut String, a format string and a context, containing Formattable values.

use interpolator::{write, Formattable};

let mut buf = String::new();
write(
    &mut buf,
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))].into_iter().collect(),
)
.unwrap();

assert_eq!(buf, format!("{:+05}", 12));

Errors

It will return an error if the specified format string has invalid syntax, the type doesn’t implement the expected trait, or the formatting itself failed.

For more details have a look at Error and ParseError.