W3cubDocs

/Rust

Function std::mem::variant_count

pub fn variant_count<T>() -> usize
🔬 This is a nightly-only experimental API. (variant_count #73662)

Returns the number of variants in the enum type T.

If T is not an enum, calling this function will not result in undefined behavior, but the return value is unspecified. Equally, if T is an enum with more variants than usize::MAX the return value is unspecified. Uninhabited variants will be counted.

Examples


use std::mem;

enum Void {}
enum Foo { A(&'static str), B(i32), C(i32) }

assert_eq!(mem::variant_count::<Void>(), 0);
assert_eq!(mem::variant_count::<Foo>(), 3);

assert_eq!(mem::variant_count::<Option<!>>(), 2);
assert_eq!(mem::variant_count::<Result<!, !>>(), 2);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/mem/fn.variant_count.html