#proc-macro #count #token

macro count-tts-inner

A simple macro for counting any position tokens

1 unstable release

new 0.1.0 May 8, 2025

#2465 in Rust patterns

MIT license

5KB

Expands to the number of token trees in the inner like macro arguments

Can be used in places where macro expansion is not possible

Similar count_tts crate

Examples

Use count_tts failed case:

use count_tts::count_tts;

macro_rules! foo {
    ($t:literal) => { $t };
}

// Expected literal, `count_tts` `!` `()` is not a literal
assert_eq!(0, foo!(count_tts!()));
assert_eq!(3, foo!(count_tts!(a b c)));
assert_eq!(2, foo!(count_tts!(a (b c))));
assert_eq!(5, foo!(count_tts!(a, b, c)));

Use this crate case:

use count_tts_inner::count_tts_inner;

macro_rules! foo {
    ($t:literal) => { $t };
}

count_tts_inner! {
    assert_eq!(0, foo!(#count_tts()));
    assert_eq!(3, foo!(#count_tts(a b c)));
    assert_eq!(2, foo!(#count_tts(a (b c))));
    assert_eq!(5, foo!(#count_tts(a, b, c)));
}

Dependencies

~26KB