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 fn
s.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"#);
}