#enums #count

macro variant_count

Derive macro for enum which adds to it the count of variants

3 stable releases

Uses new Rust 2024

1.2.0 Jun 20, 2025
1.1.0 Apr 7, 2021
1.0.0 Nov 1, 2018

#527 in Rust patterns

Download history 31560/week @ 2025-10-08 34444/week @ 2025-10-15 36761/week @ 2025-10-22 37074/week @ 2025-10-29 38285/week @ 2025-11-05 34300/week @ 2025-11-12 46398/week @ 2025-11-19 29772/week @ 2025-11-26 45466/week @ 2025-12-03 45289/week @ 2025-12-10 34525/week @ 2025-12-17 22475/week @ 2025-12-24 29821/week @ 2025-12-31 38117/week @ 2026-01-07 38521/week @ 2026-01-14 42511/week @ 2026-01-21

154,404 downloads per month
Used in 230 crates (13 directly)

MIT license

5KB

This crate provides VariantCount derive macro for enum which adds to it the VARIANT_COUNT constant, containing count of enum variants.

Example

use variant_count::VariantCount;

#[derive(VariantCount)]
enum Test {
First(i32),
Second(Option<String>),
Third,
}

assert_eq!(Test::VARIANT_COUNT, 3);

Usage

If you're using Cargo, just add it to your Cargo.toml:

[dependencies]
variant_count = "1.2"

Use VariantCount in the derive enum attribute. !


Enum variant count

Crates.io Docs

This crate provides VariantCount derive macro for enum which adds to it the VARIANT_COUNT constant, containing count of enum variants.

The VariantCount usage example:

#[derive(VariantCount)]
enum Test {
    First(i32),
    Second(Option<String>),
    Third,
}

assert_eq!(Test::VARIANT_COUNT, 3);

Usage

If you're using Cargo, just add it to your Cargo.toml:

[dependencies]
variant_count = "1.2"

License

MIT

Dependencies

~150–560KB
~13K SLoC