1 个不稳定版本

0.1.0 2022年9月7日

#16#variant-name

Download history 20/week @ 2024-04-17 28/week @ 2024-04-24 59/week @ 2024-05-01 47/week @ 2024-05-08 40/week @ 2024-05-15 63/week @ 2024-05-22 56/week @ 2024-05-29 46/week @ 2024-06-05 34/week @ 2024-06-12 62/week @ 2024-06-19 44/week @ 2024-06-26 5/week @ 2024-07-03 36/week @ 2024-07-10 105/week @ 2024-07-17 72/week @ 2024-07-24 55/week @ 2024-07-31

273 次每月下载
solwalrs 中使用

Apache-2.0

6KB
57

name_variant

生成打印枚举变体名称的方法。

示例

 use name_variant::NamedVariant;

 #[derive(NamedVariant)]
 enum TestEnum {
     A,
     B(),
     C(i32, i32),
     D { _name: String, _age: i32 },
     VariantTest,
 }

 fn main() {
     let x = TestEnum::C(1, 2);
     assert_eq!(x.variant_name(), "C");

     let x = TestEnum::A;
     assert_eq!(x.variant_name(), "A");

     let x = TestEnum::B();
     assert_eq!(x.variant_name(), "B");

     let x = TestEnum::D {_name: "Jane Doe".into(), _age: 30 };
     assert_eq!(x.variant_name(), "D");

     let x = TestEnum::VariantTest;
     assert_eq!(x.variant_name(), "VariantTest");
 }

lib.rs:

生成打印枚举变体名称的方法。

示例

use name_variant::NamedVariant;

#[derive(NamedVariant)]
enum TestEnum {
    A,
    B(),
    C(i32, i32),
    D { _name: String, _age: i32 },
    VariantTest,
}

let x = TestEnum::C(1, 2);
assert_eq!(x.variant_name(), "C");

let x = TestEnum::A;
assert_eq!(x.variant_name(), "A");

let x = TestEnum::B();
assert_eq!(x.variant_name(), "B");

let x = TestEnum::D {_name: "Jane Doe".into(), _age: 30 };
assert_eq!(x.variant_name(), "D");

let x = TestEnum::VariantTest;
assert_eq!(x.variant_name(), "VariantTest");

依赖项

~1.5MB
~36K SLoC