c0nst
Write const trait code once, run on both nightly and stable Rust.
c0nst!
Installation
[]
= "0.2"
[]
= ["c0nst/nightly"]
How it works
Replace const with c0nst - the macro transforms your code based on feature
flags:
- With
nightlyfeature:c0nst→const(modern const trait syntax) - Without
nightlyfeature:c0nstand[c0nst]are removed (stable compatibility)
Perfect for library authors - write once, let users choose between nightly const traits or stable compatibility.
Features
- Zero-cost - Simple keyword replacement, no runtime overhead
- Forward compatible - Easy migration when const traits stabilize
(
s/c0nst/const/g) - Lightweight - Single proc-macro, minimal dependencies
- Comprehensive - Handles all syntax from the RFC
- Destruct support - Automatically resolves
Destructtrait references
For Library Authors
Write const-optional traits that work for everyone! First, expose the choice to your library users:
# Cargo.toml
[]
= ["c0nst/nightly"]
Then, define and implement const traits using the nightly syntax (with the
c0nst variation):
// src/lib.rs
c0nst!
For Library Users
Runtime (Stable)
If you want to run on stable rust, use the library like normal. First, add the dependency:
# Cargo.toml
[]
= "1.0"
Then use the dependency.
// src/main.rs
let value: u32 = 42u32.calculate; // ✅ Runtime
That's it. You can compile on stable rust and get runtime behavior.
Compile-time (Nightly)
On the other hand, if you want compile-time behavior and are willing to accept
the requirement to compile only on nightly, then just use the nightly feature:
# Cargo.toml
[]
= { = "1.0", = ["nightly"] }
Then, you get compile-time behavior:
// src/main.rs
const VALUE: u32 = 42u32.calculate; // ✅ Compile-time