#string-formatting #expression #format #modifier #python #short #f-string

expression_format

A crate to format Rust expressions in a string, similar to f-string formatting in Python

7 stable releases

1.1.3 Apr 26, 2021
1.1.2 Apr 21, 2021
1.1.1 Apr 20, 2021
1.0.2 Apr 17, 2021
1.0.0 Apr 15, 2021

#272 in Value formatting

Download history 2/week @ 2024-09-14 59/week @ 2024-09-21 100/week @ 2024-09-28 58/week @ 2024-10-05 94/week @ 2024-10-12 79/week @ 2024-10-19 43/week @ 2024-10-26 167/week @ 2024-11-02 51/week @ 2024-11-09

361 downloads per month

MIT/Apache

12KB
165 lines

A crate to format and print strings with an embedded rust expression, similar to f-string formatting in Python.

Examples

Using :? modifier.

use expression_format::ex_format;
let v = vec![1, 2, 3];
assert_eq!(ex_format!("v = {:?v}"), "v = [1, 2, 3]");

Using other modifiers (std:fmt for details).

use expression_format::ex_format;
// Space after format specs if it doesn't ends in ?
assert_eq!(ex_format!(r#"Hello {:-<5 "x"}!"#), "Hello x----!");
assert_eq!(ex_format!("{:.5 12.3}"), "12.30000");
assert_eq!(ex_format!("{:#010x 27}!"), "0x0000001b!");

No support for * and $ parameters.


Printing the contents of fields.

use expression_format::ex_format;
let arg = ["ipsum", "sit"];
assert_eq!(ex_format!("lorem {arg[0]} dolor {arg[1]} amet"), "lorem ipsum dolor sit amet");

Short version of ex_format! with a complex expression.

use expression_format::short::exf;

assert_eq!(
    exf!(r#"Hello { { // Space after the first { since {{ is an escape sequence.
            let first = "Wo";
            let second = "rld";
            let mut result = String::from(first);
            result.push_str(second);
            result
     }}!"#),
    "Hello World!"
);

Print to standard output with a new line.

use expression_format::short::expl; // Short name version of ex_println!
#[derive(Debug)]
struct Point {x: i32, y: i32}

expl!("value of point = {:?Point {x: 1 + 2, y: 3 * 4 }}");
// stdout: value of point = Point { x: 3, y: 12 }

Escape brackets with {{ and }}.

use expression_format::short::exf;
let value = 10;
assert_eq!(exf!("{{value}} = {value}"), "{value} = 10");

Dependencies

~2.2–3MB
~54K SLoC