惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
GbyAI
GbyAI
S
SegmentFault 最新的问题
量子位
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
IT之家
IT之家
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
雷峰网
雷峰网

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release
Associate a struct with every enum variant
kaidezee · 2026-04-18 · via The Rust Programming Language Forum - Latest topics

April 17, 2026, 9:57pm 1

Is there a way to assign a struct constant to an enum variant?
Something like this, maybe:

struct Properties {
    thingone: bool,
    thingtwo: u32,
}

enum MyItems {
    ItemOne = Properties { thingone: false, thingtwo: 7 },
    ItemTwo = Properties { thingone: true, thingtwo: 13 },
}

To later retrieve the fields of that struct for a specific enum variant?

Enum variants are not values, they're more like types. The type associated with an enum variant can be a struct type, but not a struct value.

This can be confusing because enums in some languages are used for constants, where each constant is assigned a value.

1 Like

kyouma April 17, 2026, 10:31pm 3

I'm not sure this is the best idea, but maybe create a function on the enum?

struct Properties {
    thingone: bool,
    thingtwo: u32,
}

enum MyItems {
    ItemOne,
    ItemTwo,
}

impl MyItems {
  fn props(&self) -> Properties {
    match self {
      MyItems::ItemOne => Properties { thingone: false, thingtwo:  7 },
      MyItems::ItemTwo => Properties { thingone: true , thingtwo: 13 },
    }
  }
}

3 Likes

quinedot April 17, 2026, 10:35pm 5

Another guess at what you may want:

struct Properties {
    thingone: bool,
    thingtwo: u32,
}

impl Properties {
    const ITEM_ONE: Self = Self { thingone: false, thingtwo: 7 };
    const ITEM_TWO: Self = Self { thingone: true, thingtwo: 13 };
}

Perhaps paired with an actual enum ala @kyouma's reply.

kpreid April 17, 2026, 10:35pm 6

This is a reasonable idea. However, it is more common to write separate methods for each property:

impl MyItems {
  fn thingone(self) -> bool {
    match self {
      MyItems::ItemOne => false,
      MyItems::ItemTwo => true,
    }
  }

  fn thingtwo(self) -> u32 {
    match self {
      MyItems::ItemOne => 7,
      MyItems::ItemTwo => 13,
    }
  }
}

This way, only the needed information is returned, in one step instead of two. But, if the Properties struct is useful to the application, there is nothing wrong with doing that instead, or in addition. In that case, you might also want to consider returning a reference:

impl MyItems {
  fn props(self) -> &'static Properties {
    match self {
      MyItems::ItemOne => &Properties { thingone: false, thingtwo:  7 },
      MyItems::ItemTwo => &Properties { thingone: true , thingtwo: 13 },
    }
  }
}

This has the advantage that the Properties struct won't ever be copied around, which might have consistently better performance (depending very much on how big Properties actually is, and how it is used).

4 Likes