litext
A lightweight procedural macro library for extracting string literal contents from tokens, because doing that shouldn't bloat compilation time.
let value: String = litext!;
What is this?
Litext provides a macro to extract the inner text from a string literal token. You pass raw tokens to litext! and it gives you back the unwrapped string content without the quotes, optionally with the source span attached for precise diagnostics.
This is a proc-macro helper library. It is designed for proc-macro authors who need to extract string content from TokenStream input during macro expansion without the bloat.
Zero dependencies, tiny, built for proc-macro authors.
Installation
Add litext to your project:
Quick Start
Extract the string content as a String:
use ;
Extract with span information for precise diagnostics:
use ;
Handle errors with the try variant (returns Result instead of early return):
use ;
Return Result but for other types:
let result = litext!;
Extract other literal types directly:
use ;
Span-Aware Types
For precise error reporting, use the span-aware types:
use ;
use ;
Creating Your Own Literal Type
Implement the FromLit trait to support custom types:
use FromLit;
use ;
;
// Then use it:
// let my: MyType = litext!(input as MyType);
Features
Literal Extraction
- Extract string content from
TokenStreamtokens - Support for regular strings:
"hello world"with full escape sequences (\n,\r,\t,\\,\",\0,\x41,\u{1F600}) - Support for raw strings:
r#"hello world"#(no escape processing) - Support for raw strings with multiple hashes:
r##"hello #" world"## - Integer literals with all radixes:
42,0xFF(hex),0o77(octal),0b1010(binary) - Integer literals with underscore separators:
1_000_000 - Integer literals with type suffixes:
42_i32,100u8 - Float literals:
3.14,1e10,2.5e-3(scientific notation) - Float literals with underscore separators:
1_000.5 - Float literals with type suffixes:
3.14_f32 - Character literals with escape sequences:
'x','\n','\u{1F600}','\x41' - Boolean literals:
true,false(as identifiers) - Byte literals:
b'a',b'\n',b'\xff' - Byte string literals:
b"hello",br#"raw"# - C string literals:
c"hello",cr#"raw"#(null byte validated)
Error Handling
tryvariant returnsResult<T, TokenStream>for explicit error handling:litext!(try input)tryvariant with custom types:litext!(try input as T)- Default variant returns early on error for concise code:
litext!(input as T) - Span-accurate error reporting via
comperrintegration
Span-Aware Types
LitStr- String literal with spanLitInt<T>- Integer literal with span (supportsi8–i128,u8–u128,isize,usize; defaults toi32)LitFloat<T>- Float literal with span (supportsf32,f64; defaults tof64)LitBool- Boolean literal with spanLitChar- Character literal with spanLitByte- Byte literal with spanLitByteStr- Byte string literal with spanLitCStr- C string literal with span
Round-Tripping
ToTokenstrait for converting span-aware types back toTokenStream- Enables extract → validate → emit patterns in proc-macros
Extensibility
FromLittrait for implementing custom literal extraction- Create your own literal types by implementing
FromLit FromIdentsupport for identifier-based literals (likebool)
Limitations
- Negative numbers like
-42are not literals in Rust's token stream -- they are two tokens (a-punct and a positive literal).litextdoes not support them. Handle negation at the expression level instead.
Other
- Clear error messages for invalid inputs
- Dependencies:
proc_macro2(for non-proc-macro contexts),comperr(for error reporting) - Built for proc-macro authors
Requirements
- Rust 2024 edition
- A proc-macro crate (this library is for macro authors, not end users)
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.