litext 1.3.0

Seamless proc-macro literal extraction.
Documentation

litext

Rust Version Crates.io Version docs.rs License MIT License Apache-2.0 Crates.io Downloads Deps.rs Maintenance

Seamless literal extraction for procedural macros.

let value: String = litext!(input);
let count: u32 = litext!(input as u32);
let (s, n): (String, i32) = litext!(input as (String, i32));
let (k, v): (String, i32) = litext!(input as (String -> i32));

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

cargo add litext

Your proc-macro entry point needs to bridge proc_macro and proc_macro2:

#[proc_macro]
pub fn my_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    my_macro_inner(input.into()).into()
}

fn my_macro_inner(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
    let s: String = litext!(input);
    // ...
}

Basic Usage

// Extract any literal as String
let text: String = litext!(input);

// Extract with explicit type
let num: i32  = litext!(input as i32);
let val: f64  = litext!(input as f64);
let ch: char  = litext!(input as char);
let flag: bool = litext!(input as bool);

// Keep the Result instead of returning early
let result: Result<String, TokenStream> = litext!(try input);

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 -42 for 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 FromLit for 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_macro2 as a direct dependency

License

Licensed under either of:

at your option.

Cheers, RazkarStudio

© 2026 RazkarStudio. All rights reserved.