I have a little bit of experience with Go, that I have been trying to use as a reference point to wrap my mind around Rust via a cards game I wrote in Go that I would like to now write in Rust.
I know how I would approach this project with a language like Python or Ruby and of course Go, but I am unclear how I would approach it with Rust.
With object oriented programming for this cards game. We might decide to make something like a Deck class. A deck class is a sort of blueprint that has some number of methods and properties attached to it that describe what an instance or a copy of a deck looks like. So we can use that deck class to then create a deck instance. A deck instance might have a property called cards, which could be an array of strings. We could then make use of functions attached to the deck instance such as print, shuffle and saveToFile to manipulate that cards property attached to the deck instance.
Obviously in the Go world things are significantly different.
So as you might already know, with Go we have some of these very basic data types available to us like strings, integers and floats, but we also have arrays and slices as well, which are technically also data types inside of Go. In order to bring about this idea of a deck inside of our Go program, we can define a new type inside of Go of deck type and a deck type is essentially going to be a slice of strings and so in that slice of strings we will have our playing cards which will be a string.
Now to attach some customized functionality or some functions to work with this custom deck type we put together, we create functions called functions with receiver.
Anyway, we are going to take an existing type in the language and we are going to extend its functionality, we are going to add more properties to it. Add some functions that make use of this new type that we create. This is a very common pattern that we will see in a lot of Go programs.
So my question is, how would this cards game look like in Rust? It's design pattern that is?
So far in Rust I have something like this:
fn main() {
let mut cards = vec!["Ace of Diamonds", new_card()];
let additional_card: [&str; 1] = ["Six of Spades"];
cards.extend(additional_card);
// or for a single card
// cards.push("A single card");
for i in cards {
println!("{i} cards");
}
}
fn new_card() -> &'static str {
"Five of Diamonds"
}
which is somewhat similar syntactically to its equivalent in Go, but again I am looking for some understanding on what the design pattern would be for Rust for such a game.
I will try to be more specific, in Go, we may want to create a new type of deck which is a slice of strings. Can a similar approach be expected in Rust? Is the concept of a slice of strings similar?