rtformat
Runtime string formatting for Rust, with std::fmt-compatible placeholder syntax.
Rust's built-in format! macro requires the format string to be a compile-time
literal. rtformat lets you take a template string at runtime — from a config
file, a database, or user input — and format it with the same placeholder syntax
you already know.
Features
{}implicit positional arguments;{0}/{1}explicit indices (reusable)- Types:
{:?}{:#?}{:x}{:X}{:o}{:b}{:e}{:E} - Alignment and fill:
{:<10}{:>10}{:^10}{:_>10} - Sign / radix prefix / zero padding:
{:+}{:#x}{:010} - Width and precision:
{:.3}{:1$}{:.1$}(n$takes the width/precision from argumentn) {{/}}escapes- Custom argument types via
#[derive(FormatArg)] - Fallible formatting (
try_format) and an incremental builder API
Not supported: named arguments {name}, pointers {:p}, hexadecimal debug
{x?} / {X?}.
Usage
Add the dependency:
[]
= "0.1"
The rformat! macro
use rformat;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The Format trait
Format is implemented for str (and available on String, Box<str>, etc.
through deref):
use Format;
assert_eq!;
// Fallible variants, for templates that may be invalid:
let result = "{}".try_format;
assert_eq!;
Builder API
Add arguments one at a time:
use Format;
let s = "{} + {} = {2}".builder.arg.arg.arg.build;
assert_eq!;
Custom argument types
Types implementing Display and/or Debug can derive FormatArg. The derive
adapts: {} prefers Display and falls back to Debug, while {:?} / {:#?}
require Debug:
use fmt;
use ;
;
assert_eq!;
For full control over each format type, implement FormatArg manually instead.
Prelude
All commonly used items are available through the prelude:
use *;
assert_eq!;
Workspace layout
| Crate | Description |
|---|---|
rtformat |
The formatting engine, traits, and the rformat! macro |
rtformat-derive |
The #[derive(FormatArg)] proc-macro |
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.