[](https://github.com/rodrimati1992/tstr_crates/actions)
[](https://crates.io/crates/tstr)
[](https://docs.rs/tstr/*)
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 `char` const parameters.
- items for converting type-level strings to `&'static str` and `&'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.
```rust
use std::ops::Index;
use tstr::{TS, ts};
fn main(){
takes_person(&Person::new("Bob".into(), "Marley".into()));
takes_person(&OtherPerson::new("Bob", "Marley"));
}
fn takes_person<P>(pers: &P)
where
P: Index<TS!(name), Output = str> + Index<TS!(surname), Output = str>
{
assert_eq!(&pers[ts!(name)], "Bob");
assert_eq!(&pers[ts!(surname)], "Marley");
}
use person::Person;
mod person {
use std::ops::Index;
use tstr::TS;
pub struct Person {
name: String,
surname: String,
}
impl Person {
pub fn new(name: String, surname: String) -> Self {
Self{name, surname}
}
}
impl Index<TS!(name)> for Person {
type Output = str;
fn index(&self, _: TS!(name)) -> &str {
&self.name
}
}
impl Index<TS!(surname)> for Person {
type Output = str;
fn index(&self, _: TS!(surname)) -> &str {
&self.surname
}
}
}
use other_person::OtherPerson;
mod other_person {
use std::ops::Index;
use tstr::TS;
pub struct OtherPerson {
name: &'static str,
surname: &'static str,
}
impl OtherPerson {
pub fn new(name: &'static str, surname: &'static str) -> Self {
Self{name, surname}
}
}
impl Index<TS!(name)> for OtherPerson {
type Output = str;
fn index(&self, _: TS!(name)) -> &str {
self.name
}
}
impl Index<TS!(surname)> for OtherPerson {
type Output = str;
fn index(&self, _: TS!(surname)) -> &str {
self.surname
}
}
}
```
### Type errors
This example showcases what TStr looks like in simple type errors.
```rust,compile_fail
let _: tstr::TS!("Hello, world!") = ();
```
With no crate features enabled, the error message is this:
```text
error[E0308]: mismatched types
--> tstr/src/lib.rs:114:37
|
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):
Enables [`const_panic`] reexports, assertion macros,
and `const_panic::fmt::PanicFmt` impl for `TStr`.
- `"use_syn"`(disabled by default):
Changes how literals passed to the macros of this crate are parsed to use the `syn` crate.
Use this if there is some literal that could not be
parsed but is a valid str/integer literal.
- `"serde"`(disabled by default):
Enables serde dependency and implements `serde::{Serialize, Deserialize}` for `TStr`
- `"str_generics"`(disabled by default):
Changes the representation of type-level strings to use a `&'static str` const parameter,
making for better compiler errors.
As of 2025-08-18, this feature can't be enabled, because it
requires `&'static str` to 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 str` const 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.
[`TStr`]: https://docs.rs/tstr/*/tstr/struct.TStr.html
[`const_panic`]: https://docs.rs/const_panic/0.2/const_panic/
[`Index`]: https://doc.rust-lang.org/std/ops/trait.Index.html
[`tstr::utils`]: https://docs.rs/tstr/*/tstr/utils/index.html