Data, structs, statics const

Hi, I'm reasonably new to rust and I have a question. If I wanted to have a collection (assuming vec) of structs, with predefined fixed fields... (Although the exact values for the fields will vary across these structs)... For example, probably the best analogy is a deck of cards... (But in this case with multiple field such as different scalers, enums etc)... And this collection and its contents must remain constant for the life of the program (immutable)...
What would be my best approach... ?
Hard code the structs, put them in a lib/mod file, name them card1 through ... whatever ... Etc... push them on the vector via a loop? Static or const?...
Or is there another way.? I guess I could construct them with a CSV, or json, RON parse... But I would rather the stuct info was embedded into the binary (hope my understanding is right)l here) like a string literal / slice. I.e. in a special place in memory... :slight_smile:

I would like to shuffle etc... But not change.

Thanks in advance for any help.
JM

If the fields are fixed it sounds like you just want a struct. HashMaps other more complex data structures are normally used when you don't know the keys/fields upfront, or where they may change dynamically as your code is executed.

How you construct them is totally dependent on your application, so you'll need to create custom functions that parse your input from whatever format it happens to be in. If the values are created programmatically (e.g. you use loops to construct every possible card in a deck) then you might store the values by pushing them into a vector.

What have you tried so far?

1 Like

Thanks for the reply,
After I wrote this down... And used the deck of cards analogy... (A brain fart) and I found a deck of cards program in rust.

There they created an array of cards, hard coded, with enums... I can sort of use this principle, expanded... (My fields are different). The only thing is, I guess, I have to make this static rather than constant. I have 109 cards, with 15 fields per card.... So I guess this would be considered too big for const and with static I get the preallocated memory I want.
Does that sound reasonable?
I might of course be going mad!

There are no limits to the size a static or const variable can have.

In the embedded world, it's pretty common to create large static buffers that can be reused throughout the program.

Generally, you will only use static variables when you need your program to have exactly one instance of that variable and a stable address.

Also, let's do some back of the envelope maths here...

  • For simplicity, let's assume each field is a u64 (8 bytes)
  • One "card" will require 15 * 8 = 120 bytes
  • 109 cards will require 109 * 120 = 13,080 bytes = 13 kb
  • My laptop has 16 GB of RAM - about 16,000,000,000 bytes
  • That means this entire deck of cards would take up approximately 0.000082% of my laptop's available memory

It's not that you are going mad, it's that you are focusing on the wrong thing. The hardware in a modern computer is crazy powerful, so instead of trying to save a couple of bytes, you should be optimising for developer time.

It's going to take you a good 10 minutes to type out a static variable with 109 cards, whereas writing a 5 line for-loop that generates all the different cards might take you a minute. Similarly, that 5 line for-loop is going to be a hell of a lot easier to maintain (or fix when you find it has a bug) than a 109*15 = 1635 line array literal.

Obviously, your circumstances may be different and add their own requirements, but the general principle of optimising for ease of development and maintenance will almost always hold.

2 Likes

Thanks for the help.
I think it have enough to proceed.
All the best,

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.