1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rustマクロ冬期講習Advent Calendar 2024

Day 21

Rust deriveマクロを軽くハンズオン

Posted at

こちらの記事は Rustマクロ冬期講習アドベントカレンダー 21日目の記事です!

前前回は関数風マクロについて、 前回 は属性風マクロについて軽いハンズオンを行いました。今回はderiveマクロに取り組みます!

deriveマクロ「 EnumDisplay

ド定番な車輪ですが、列挙体に std::fmt::Display を実装する EnumDisplay マクロを作ってみたいと思います!

Rust
#[derive(EnumDisplay)]
#[enum_display(rename_all = "snake_case")]
enum Hoge {
    Fuga,
    #[enum_display(rename = "barbar")]
    Bar,
}

こう書くと Display が自動実装されることを目指します!

Rust
fn main() {
    println!("{} {}", Hoge::Fuga, Hoge::Bar);
    // hoge barbar
}

(鋭意編集中!)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?