this post was submitted on 12 Jul 2025
14 points (100.0% liked)

Rust

7158 readers
25 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

An interesting article that lays out a problem and goes through a few different solutions, some of which I haven't thought about much before.

I'm working on a video game in Rust, and I'm running into this kind of modelling problem when it comes to keeping track of the game state. So far I've refactored something that resembles Approach 5 into something that looks more like Approach 3. As I get more confident about the final shape of the data, it (seemingly) becomes a better idea to represent it in a more structured way.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] BB_C@programming.dev 3 points 1 day ago (1 children)

I only skimmed this. But my mind from the start immediately went to

struct CommonData {
 // common fields
}

enum VariantData {
  Variant1 {
    // Variant1 specific fields
  },
  // same for other variants
}

struct Search {
  common: CommonData,
  variant: VariantData,
}

but I reached the end and didn't see it.

Isn't this the way that comes to mind first for others too?

[โ€“] tatterdemalion@programming.dev 2 points 1 day ago* (last edited 1 day ago)

It's approach 2