Function interpolator::write

source ·
pub fn write(
    out: &mut impl Write,
    format: &str,
    context: &impl Context
) -> 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::<HashMap<_, _>>(),
)
.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.