Function interpolator::format

source ·
pub fn format(format: &str, context: &impl Context) -> Result<String, Error>
Expand description

Runtime version of format!.

Takes a string and a context, containing Formattable values, returns a string.

use interpolator::{format, Formattable};

let formatted = format(
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))]
        .into_iter()
        .collect::<HashMap<_, _>>(),
)
.unwrap();

assert_eq!(formatted, 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.