litext
Seamless literal extraction for procedural macros.
let value: String = litext!;
let count: u32 = litext!;
let : = litext!;
let : = litext!;
What is this?
litext (lee-text /ˈliː.tɛkst/) is a crate for extracting literal values for procedural macros. It's clean, easy, extendable, and fits in a singular line.
When you write a procedural macro, you often receive a TokenStream that contains a single literal and need to pull out its value. litext does this for you, easily, in one line.
The library supports every Rust literal kind, span-aware wrapper types for precise diagnostics, a ToTokens round-trip trait, a FromLit extension point for custom types (that you can make!), and a variadic multi-extraction form.
Installation
Your proc-macro entry point needs to bridge proc_macro and proc_macro2:
Basic Usage
// Extract any literal as String
let text: String = litext!;
// Extract with explicit type
let num: i32 = litext!;
let val: f64 = litext!;
let ch: char = litext!;
let flag: bool = litext!;
// Keep the Result instead of returning early
let result: = litext!;
See MORE.md for detailed documentation on multi-extraction, span-aware types, round-tripping, custom types, and all supported literal formats.
Why litext?
- Every literal kind: strings, integers in any radix, floats, chars, bools, bytes, byte strings, C strings
- Negative numbers: handles
-42for signed types automatically - Extract multiple at once: pull several values from one TokenStream with a clean tuple syntax
- Flexible separators: single-char (
,,;) or two-char (->,=>,::,..) separators - Span-aware: every type has a
Lit*variant with.span()for precise error messages - Round-trip support: convert back to TokenStream with original spans preserved
- Extendable: implement
FromLitfor your own types
Supported Types
| Category | Types |
|---|---|
| Strings | String, LitStr |
| Integers | i8-i128, isize, u8-u128, usize, LitInt<T> |
| Floats | f32, f64, LitFloat<T> |
| Characters | char, LitChar |
| Booleans | bool, LitBool |
| Bytes | u8, LitByte, Vec<u8>, LitByteStr |
| C Strings | CString, LitCStr |
As of 1.2, negative literals work for signed types (i32, f64, etc. not u32, etc.)
Requirements
- Rust 2024 edition (Rust 1.85)
proc_macro2as a direct dependency
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Cheers, RazkarStudio
© 2026 RazkarStudio. All rights reserved.