const_format

Compile-time string formatting and concatenation for Rust, producing static strings.

Library
Cargo
v0.2.36
275stars
Zlib

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
38/100Needs Attention
Development Activity12
Maintenance20
Community40
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture84
Code Quality85
Innovation88
Learning Curve70

const_format is a Rust crate that brings string formatting and manipulation to compile time. Its macros concatenate and format integer, bool, char, and string constants into &'static str values that are fully evaluated during compilation, with zero runtime cost and full no_std support.

Beyond format!-style concatenation via concatcp! and formatcp!, it offers compile-time string operations — indexing, repetition, splicing, replacement, case conversion, and splitting — plus compile-time assertion macros. An optional fmt feature unlocks a std::fmt-like API for formatting your own types in const contexts, making it a go-to tool for building constant identifiers, error messages, and configuration strings at build time.

What You Get

  • concatcp! and formatcp! for compile-time concatenation and format!-style string building from primitive constants
  • Compile-time string operations: str_get, str_index, str_repeat, str_splice, str_replace, str_split, and map_ascii_case
  • Compile-time assertion macros (assertcp!, assertcp_eq!, assertcp_ne!) for const-evaluated checks
  • An optional std::fmt-like API (fmt feature) for formatting custom types in const contexts
  • Full no_std support with all formatting resolved at compile time and zero runtime overhead

Common Use Cases

  • Building constant identifiers, paths, or error messages from other constants at compile time
  • Generating static configuration or version strings without runtime allocation
  • Enforcing invariants on string constants with compile-time assertions

Under The Hood

Architecture The repo is a Cargo workspace whose members are the public const_format crate, a const_format_proc_macros procedural-macro crate that does the heavy lifting of parsing and code generation, and helper print_errors/print_warnings crates used for testing macro diagnostics. User-facing macros in const_format delegate to the proc-macro crate to evaluate concatenation, formatting, and string operations as compile-time constants, emitting &'static str values.

Tech Stack Pure Rust (99.97% of the codebase) organized as a workspace with resolver 2. Features are version-gated — core macros work on Rust 1.71, while the fmt feature that adds a std::fmt-like const API requires Rust 1.83 for mutable references in const contexts. Optional features include assertcp and fmt.

Code Quality The crate maintains dedicated tests directories plus the print_errors/print_warnings harnesses that assert on compiler diagnostics — a rigorous approach for a macro-heavy library where error messages matter. A detailed Changelog and per-version feature documentation reflect careful maintenance despite low recent commit activity.

API Design Macro names follow a consistent *cp (const primitive) and str_* convention that signals exactly what each does, and formatcp! deliberately mirrors the standard format! syntax to minimize surprise. The main friction is Rust’s own const-evaluation constraints — which types are allowed and which Rust version unlocks which feature — so there is a moderate learning curve around the boundaries rather than the API surface itself.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search