[][src]Crate fstrings

::fstrings

Repository Latest version Documentation License

Basic fstring interpolation in Rust

The interpolation works as follows:

  1. if the (template) string literal contains a named parameters (e.g. {name})

  2. and no name = value argument is fed to the formatting call,

  3. then an automatic name = name argument is added, so that the variable is effectively interpolated from the current scope.

Example

#[macro_use]
extern crate fstrings;

fn main ()
{
    let name = "World";

    // Usage is simple: just append `_f` to the name of any formatting macro
    println_f!("Hello, {name}!");

    assert_eq!(
        f!("Hello, {name}!"), // shorthand for String creation (Python-like)
        String::from("Hello, World!"),
    );

    // advanced_cases
    {
        // it remains compatible with classic formatting parameters
        assert_eq!(
            f!("{hi}, {name}!", hi = "Hello"),
            "Hello, World!",
        );

        // you can override / shadow the named arguments
        assert_eq!(
            f!("Hello, {name}!", name = "Earth"),
            "Hello, Earth!",
        );
    }
}

Macros

eprint_f

Like eprint!, but with basic f-string interpolation.

eprintln_f

Like eprintln!, but with basic f-string interpolation.

f

Shorthand for format_f.

format_args_f

Like format_args!, but with basic f-string interpolation.

format_f

Like format!, but with basic f-string interpolation.

print_f

Like print!, but with basic f-string interpolation.

println_f

Like println!, but with basic f-string interpolation.

write_f

Like write!, but with basic f-string interpolation.

writeln_f

Like writeln!, but with basic f-string interpolation.