If you'd like to learn Rust, The Book from Rust's official website, is freely available and quite good.
I've gotten to chapter 10ish and have adapted the book's guessing game to flex my newfound skills. Here's that game.
secret_numbers.rs
defines the SecretNumber
struct, representing a guessable secret number and Accuracy
, representing how arbitrarily close to the guess a given number is.
Some interesting points are the derived traits automatically provided by the compiler by decorating the Accuracy
enum with #[derive(PartialEq, Eq, Hash)]
.
PartialEq
andEq
implements functionality to compare values and, well - if you don't have it (or your own implementation), the compuler will tell you
error[E0277]: can't compare
secret_number::Accuracy
withsecret_number::Accuracy
--> src\secret_number.rs:12:10 | 12 | #[derive(Eq, Hash, Debug)] | ^^ no implementation forsecret_number::Accuracy == secret_number::Accuracy
| = help: the traitstd::cmp::PartialEq
is not implemented forsecret_number::Accuracy
Hash
implements the supporting functionality to put values in a HashMap (a Dictionary).
main.rs
is the game itself, providing IO functionality and some randomness to make the game more interesting.
prompt
asks the user a question, ensuring that the answer is between a given range of values.random_prompt_string
generates a semi-random question to ask.main
is the game loop.