This is a number guessing game written in Rust.
The code is taken from chapter 2 of The Rust Programming Language by Steve Klabnik and Carol Nichols (online version and print version available).
The book uses the code to show some of the Rust language and environment features. I've picked out a few of these that I find interesting and written about them.
Here are a few of my picks:
cargo new
creates a new project and creates a Git repository.- Pattern matching with
match
. - Error handling:
- Functions can return a Result type with an "Ok" / "Err" enum.
.expects("something bad happened")
to include a message when an error happens (unhandled error).- Pattern matching on the Result lets you handle an error.
- Variables are immutable by default and must be marked "mut" explicitly if the values can be changed.
- Variables can be shadowed, which allows us to reuse variable names (this seems much better when seen in context).