Learning Rust, Day 1
I decided to start learning Rust and wanted to try something I’ve never done: write about it while learning.
The Idea
- Start out with some basic reading
- Use my favorite card game, cribbage, as a template for working out basic algorithms, data structures, and general syntax
Notes while reading
- Oof, I’m gonna have to get used to static typing again.
- References and borrowing are wacky! It’s intuitive in the “follow the scope” sense but I’m going to have to just work with it more.
- Lifetimes are a clever way to get around “the compiler can’t figure this out” cases, if not clunky syntax-wise.
- Obligatory “strings are hard” section in the tutorial.
match
makes sense but feels weak compared to Elixir’s pattern matching.- The Rust book is very good so far. I stopped on the chapter about testing; I’ll have to pick up the rest later.
Working with code
I didn’t get very far today. This is an abbreviated excerpt:
struct Card {
rank: Rank,
suit: Suit
}
enum Rank {
Two,
Three,
# etc
King,
Ace
}
enum Suit {
Hearts,
Spades,
Diamonds,
Clubs
}
impl Card {
fn new(rank: Rank, suit: Suit) -> Card {
Card {
rank,
suit
}
}
}
Some notes on this:
- Tests are confusing; they effectively recommend setting up a
mod test
block in each file for the unit tests. I like the idea of keeping them close to what they test, but I’ve had trouble lining up the varioususe
andmod
calls to get it wired up. - The compiler messages are extremely good.
- The documentation on the difference between a binary and library project are kind of strange? Maybe I’m missing it, I need to just set up both.
- Convention over configuration! Stuff like
mod
referring to the file name,rustfmt
, etc. - Compile directives are…janky. Stuff like
#[derive(Debug)]
and#[test]
feel bolted on.
That’s all for now!