An encoding of type-level strings, with the TStr type and related macros.
This crate features all these on stable:
- a relatively readable default representation of type-level strings
based on
charconst parameters. - items for converting type-level strings to
&'static strand&'static [u8] - functions for comparing type-level strings to each other and
&str - macros for asserting the (in)equality of type-level strings to each other and
&str
All of the above functionality can be used in const contexts.
Examples
Indexing
This example demonstrates how you can use type-level strings,
and the Index trait, to access fields of generic types by name.
use Index;
use ;
use Person;
use OtherPerson;
Type errors
This example showcases what TStr looks like in simple type errors.
let _: TS! = ;
With no crate features enabled, the error message is this:
error[E0308]: mismatched types
--> tstr/src/lib.rs:114:37
|
5 | let _: tstr::TS!("Hello, world!") = ();
| -------------------------- ^^ expected `TStr<___<..., 13>>`, found `()`
| |
| expected due to this
|
= note: expected struct `tstr::TStr<___<(tstr::__<'H', 'e', 'l', 'l', 'o', ',', ' ', 'w'>, tstr::__<'o', 'r', 'l', 'd', '!'>, (), (), (), (), (), ()), 13>>`
found unit type `()`
As you can see, the string is represented as a collection of char const parameters.
When the "nightly_str_generics" feature is enabled (which requires the nightly compiler),
the error message is this:
error[E0308]: mismatched types
--> tstr/src/lib.rs:114:37
|
5 | let _: tstr::TS!("Hello, world!") = ();
| -------------------------- ^^ expected `TStr<___<"Hello, world!">>`, found `()`
| |
| expected due to this
|
= note: expected struct `tstr::TStr<___<"Hello, world!">>`
found unit type `()`
Macro expansion
This library reserves the right to change how it represent type-level strings internally in every single release, and cargo feature combination.
This only affects you if you expand the code generated by macros from this crate, and then use that expanded code instead of going through the macros.
Cargo features
-
"const_panic"(enabled by default): Enablesconst_panicreexports, assertion macros, andconst_panic::fmt::PanicFmtimpl forTStr. -
"use_syn"(disabled by default): Changes how literals passed to the macros of this crate are parsed to use thesyncrate. Use this if there is some literal that could not be parsed but is a valid str/integer literal. -
"str_generics"(disabled by default): Changes the representation of type-level strings to use a&'static strconst parameter, making for better compiler errors. As of 2025-08-18, this feature can't be enabled, because it requires&'static strto be stably usable as const parameters. Consider using"nightly_str_generics"if this feature can't be used. -
"nightly_str_generics"(disabled by default): Equivalent to the"str_generics"feature, and enables the nightly compiler features to use&'static strconst parameters.
No-std support
This crate is unconditionally #![no_std], and can be used anywhere that Rust can be.
Minimum Supported Rust Version
This crate supports Rust versions back to Rust 1.88.0.