dyn_formatting 3.0.0

A library provides limited Python-style safe dynamic (runtime) formatting support for Rust
Documentation
  • Coverage
  • 100%
    12 out of 12 items documented1 out of 3 items with examples
  • Size
  • Source code size: 14.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cup113

Dynamic Formatting README

Lightweight, dynamic, Python-styled string formatting (Only support String, {key} patterns). It only needs std to work.

Escape like {{ or }}.

Install

cargo add dyn_formatting@3.0.0

Examples

use dyn_formatting::dynamic_format;
assert_eq!(
    dynamic_format(
        "I'm {name}. I'm {age} years old now.",
        &[("name", "ABC"), ("age", "20")].into()
    ).unwrap(),
    "I'm ABC. I'm 20 years old now.".to_string()
);
use dyn_formatting::dynamic_format;
use std::collections::HashMap;
let value_age = (15).to_string(); // Make lifetime long enough
let dictionary = HashMap::from([
    ("age", value_age.as_str()),
]);
assert_eq!(
    dynamic_format("{{{age} }}{age}", &dictionary).unwrap(),
    "{15 }15"
)
use dyn_formatting::dynamic_format;
assert!(
    dynamic_format(
        "I'm {name}. I'm {age} years old now.",
        &[("name", "ABC")].into()
    ).is_err() // Key error
);
use dyn_formatting::dynamic_format;
assert!(
    dynamic_format(
        "I'm {name{name}}.",
        &[("name", "ABC")].into()
    ).is_err() // Token error: '{' unmatched.
);