Best data type to use in this case


Say I have hundreds of structs that look like this:

struct doge {
  x: u8
  y: u32
  z: String
  a: u16

In this, doge.x can have any value between 1 and 255.
Each value is associated with a different meaning/message.

Consider that I'll have 2 programs - one to parse some binary data (i.e., read fields like doge.x), and another to make the parsed data human-friendly.

For the 2nd program, is it ideal to store the associated meaning aka message text in a HashMap, serving as a look-up table, so that when printing out a report, it'll say x - message text for x rather than x - 1.
I understand that this probably means making many many copies of that message text when printing out.

HashMap with content like:

doge.x.1  -  message for text doge.x.1
doge.x.2  -  message for text doge.x.2
doge.x.3  -  message for text doge.x.3
doge.x.4  -  message for text doge.x.4
doge.x.255 - message for text doge.x.255

Is there a better way of doing this?
Please do let me know if any clarification is needed in the scenario I've explained above.

Thanks as always for your feedback.

If I needed to store a lookup table for 255 contiguous values, I'd put them in a static array (or slice).

static DOGE_NAME: &'static [&'static str] = &[
    // etc

Side note, if you don't mind the ergonomic hit in exchange for some type safety and possible optimizations, you might want to use NonZeroU8.

Ok, it's not always 0-255.
If it was, an array will work, wherein DOGE_NAME[12] will refer to the 12th item (message) in the array.

As the numbers won't always be in a sequence, I'd need to have a key (usually a number) and a value (some message text).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.