this post was submitted on 19 Jun 2023
4 points (100.0% liked)
Rust Programming
8191 readers
3 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
You'll notice that new is not a keyword or any special rust thing. It's actually a software design pattern called a smart constructor, and validation is part of the intent. Pattern wise putting this into a smart constructor and encapsulating changes to this value is, I believe, idiomatic.
IIUC rust does not have a type system where you can give numeric bounds. Creating one would likely mean you'd need to define all the maths, which is likely not worth it.
You reminded me of a deeply amusing story of solving the 4 queens problem with just the type system.
I was actually vaguely thinking that for playing cards, there's no actual way to do maths on it (like you can't add an Ace to a Jack, or a Joker to a Queen). Are Aces high? Is the 2 worth 2 points or something else? So maybe OP is better off just defining the ranks manually (A, 2, 3 ... J, Q K) and then separate out any points calculations. In that sense, OP might actually want the game rules encoded into the type system.
EDIT: Also, nice, I remember reading that story.