6 releases

0.1.5 Mar 17, 2021
0.1.4 Jun 18, 2020
0.1.3 May 28, 2020
0.1.2 Aug 23, 2019
0.1.1 Jan 18, 2019

#565 in Parser implementations

Download history 5234/week @ 2024-03-14 8068/week @ 2024-03-21 5249/week @ 2024-03-28 7341/week @ 2024-04-04 5940/week @ 2024-04-11 6314/week @ 2024-04-18 6530/week @ 2024-04-25 3144/week @ 2024-05-02 2588/week @ 2024-05-09 2897/week @ 2024-05-16 2470/week @ 2024-05-23 2787/week @ 2024-05-30 2788/week @ 2024-06-06 3166/week @ 2024-06-13 3102/week @ 2024-06-20 2313/week @ 2024-06-27

11,812 downloads per month
Used in 9 crates (8 directly)

MIT license

1.5K SLoC

dynfmt - Dynamic Formatting in Rust

A crate for formatting strings dynamically.

dynfmt provides several implementations for formats that implement a subset of the std::fmt facilities. Parsing of the format string and arguments checks are performed at runtime. There is also the option to implement new formats.

The public API is exposed via the Format trait, which contains formatting helper functions and lower-level utilities to interface with format strings. See the Features section for a list of provided implementations.


use dynfmt::{Format, NoopFormat};

let formatted = NoopFormat.format("hello, world", &["unused"]);
assert_eq!("hello, world", formatted.expect("formatting failed"));

See the Format trait for more methods.


This crate ships with a set of features that either activate formatting capabilities or new format implementations:

  • json (default): Implements the serialization of complex structures via JSON. Certain formats, such as Python, also have a representation format (%r) that makes use of this feature, if enabled. Without this feature, such values will cause an error.
  • python: Implements the printf-like format that python 2 used for formatting strings. See PythonFormat for more information.
  • curly: A simple format string syntax using curly braces for arguments. Similar to .NET and Rust, but much less capable. See SimpleCurlyFormat for mor information.


Implement the Format trait to create a new format. The only required method is iter_args, which must return an iterator over ArgumentSpec structs. Based on the capabilities of the format, the specs can be parameterized with formatting parameters.

use std::str::MatchIndices;
use dynfmt::{ArgumentSpec, Format, Error};

struct HashFormat;

impl<'f> Format<'f> for HashFormat {
    type Iter = HashIter<'f>;

    fn iter_args(&self, format: &'f str) -> Result<Self::Iter, Error<'f>> {

struct HashIter<'f>(MatchIndices<'f, char>);

impl<'f> Iterator for HashIter<'f> {
    type Item = Result<ArgumentSpec<'f>, Error<'f>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|(index, _)| Ok(ArgumentSpec::new(index, index + 1)))

let formatted = HashFormat.format("hello, #", &["world"]);
assert_eq!("hello, world", formatted.expect("formatting failed"));

License: MIT


~36K SLoC