Const_format 0.2: compile-time string formatting

The const_format crate allows you to do compile-time string formatting.

It supports Rust 1.46 exporting the concatcp and formatcp macros, which can format some primitive types into &'static str constants.

If you use nightly, you can format user-defined types, with a formatting API that's comparable to the one in the standard library(with a few limitations).
This uses mutable references in const fns.When those are stabilized, formatting user-defined types won't require nightly anymore.

Example:

use const_format::formatcp;

const FOO: &str = "hello";
const STR: &str = formatcp!("{:?} {:#b}", FOO, FOO.len());

fn main(){
    assert_eq!(STR, r#""hello" 0b101"#);
}
9 Likes

A good one. Thanks!

At times it comes handy, I remember that a few times I needed to create a const string from previously declared constants.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.