Compile-time string formatting.
This crate provides types and macros for formatting strings at compile-time.
Rust versions
There are some features that require Rust 1.46.0, some that require Rust 1.51.0, and others that require Rust nightly, the sections below describe the features that are available for each version.
Rust 1.46.0
These macros are the only things available in Rust 1.46.0:
-
concatcp: Concatenatesintegers,bool,char, and&strconstants into a&'static strconstant. -
formatcp:format-like formatting which takesintegers,bool,char, and&strconstants, and emits a&'static strconstant. -
str_get: Indexes a&'static strconstant, returningNonewhen the index is out of bounds. -
str_index: Indexes a&'static strconstant. -
str_repeat: Creates a&'static strby repeating a&'static strconstanttimestimes. -
str_splice: Replaces a substring in a&'static strconstant.
Rust 1.51.0
By enabling the "const_generics" feature, you can use these macros:
-
map_ascii_case: Converts a&'static strconstant to a different casing style, determined by aCaseargument. -
str_replace: Replaces all the instances of a pattern in a&'static strconstant with another&'static strconstant.
Rust 1.57.0
The "assertcp" feature enables the assertcp, assertcp_eq,
and assertcp_ne macros.
These macros are like the standard library assert macros,
but evaluated at compile-time,
with the limitation that they can only have primitive types as arguments
(just like concatcp and formatcp).
Rust nightly
By enabling the "fmt" feature, you can use a std::fmt-like API.
This requires the nightly compiler because it uses mutable references in const fn, which have not been stabilized as of writing these docs.
All the other features of this crate are implemented on top of the const_format::fmt API:
-
concatc: Concatenates many standard library and user defined types into a&'static strconstant. -
formatc:format-like macro that can format many standard library and user defined types into a&'static strconstant. -
writec:write-like macro that can format many standard library and user defined types into a type that implementsWriteMarker.
The "derive" feature enables the ConstDebug macro,
and the "fmt" feature.
ConstDebug derives the FormatMarker trait,
and implements an inherent const_debug_fmt method for compile-time debug formatting.
The "assertc" feature enables the assertc, assertc_eq, assertc_ne macros,
and the "fmt" feature.
These macros are like the standard library assert macros, but evaluated at compile-time.
Examples
Concatenation of primitive types
This example works in Rust 1.46.0.
use concatcp;
const NAME: &str = "Bob";
const FOO: &str = concatcp!;
assert_eq!;
Formatting primitive types
This example works in Rust 1.46.0.
use formatcp;
const NAME: &str = "John";
const FOO: &str = formatcp!;
assert_eq!;
const
Formatting custom types
This example demonstrates how you can use the ConstDebug derive macro,
and then format the type into a &'static str constant.
This example requires Rust nightly, and the "derive" feature.
use ;
;
const MSG: Message = Message;
const FOO: &str = formatc!;
assert_eq!;
Formatted const assertions
This example demonstrates how you can use the assertcp_ne macro to
do compile-time inequality assertions with formatted error messages.
This requires the "assertcp" feature,
because using the panic macro at compile-time requires Rust 1.57.0.
use assertcp_ne;
check_valid_pizza!;
check_valid_pizza!;
check_valid_pizza!;
#
This is the compiler output:
error[E0080]: evaluation of constant value failed
--> src/lib.rs:178:27
|
20 | check_valid_pizza!("Bob", "pineapple");
| ^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left != right)`
left: `"pineapple"`
right: `"pineapple"`
You can't put pineapple on pizza, Bob
', src/lib.rs:20:27
Limitations
All of the macros from const_format have these limitations:
-
The formatting macros that expand to
&'static strs can only use constants from concrete types, so while aType::<u8>::FOOargument would be fine,Type::<T>::FOOwould not be (Tbeing a type parameter). -
Integer arguments must have a type inferrable from context, more details in the Integer arguments section.
-
They cannot be used places that take string literals. So
#[doc = "foobar"]cannot be replaced with#[doc = concatcp!("foo", "bar") ].
Integer arguments
Integer arguments must have a type inferrable from context. so if you only pass an integer literal it must have a suffix.
Example of what does compile:
const N: u32 = 1;
assert_eq!;
assert_eq!;
Example of what does not compile:
assert_eq!(const_format::concatcp!(1 + 1, 2 + 1), "23");
Plans
None right now.
Renaming crate
All function-like macros from const_format can be used when the crate is renamed.
The ConstDebug derive macro has the #[cdeb(crate = "foo::bar")] attribute to
tell it where to find the const_format crate.
Example of renaming the const_format crate in the Cargo.toml file:
= { = "0.*", = "const_format"}
Cargo features
-
"fmt": Enables the
std::fmt-like API, requires Rust nightly because it uses mutable references in const fn. This feature includes theformatc/writecformatting macros. -
"derive": implies the "fmt" feature, provides the
ConstDebugderive macro to format user-defined types at compile-time. This implicitly uses thesyncrate, so clean compiles take a bit longer than without the feature. -
"assertc": implies the "fmt" feature, enables the
assertc,assertc_eq, andassertc_neassertion macros. This feature was previously named "assert", but it was renamed to avoid confusion with the "assertcp" feature. -
"assertcp": Requires Rust 1.57.0, implies the "const_generics" feature. Enables the
assertcp,assertcp_eq, andassertcp_neassertion macros. -
"constant_time_as_str": implies the "fmt" feature. An optimization that requires a few additional nightly features, allowing the
as_bytes_altmethods andslice_up_to_len_altmethods to run in constant time, rather than linear time proportional to the truncated part of the slice. -
"const_generics": Requires Rust 1.51.0. Enables the macros listed in the Rust 1.51.0 section. Also changes the the implementation of the
concatcpandformatcpmacros to use const generics.
No-std support
const_format is #![no_std], it can be used anywhere Rust can be used.
Minimum Supported Rust Version
const_format requires Rust 1.46.0, because it uses looping an branching in const contexts.
Features that require newer versions of Rust, or the nightly compiler, need to be explicitly enabled with cargo features.