Boom or Bust, 50 Days to Colorado Gold Rust: Day 2

Shawn Bachlet
3 min readAug 5, 2019

Sometimes the best way to get started learning a new language is to dive headfirst into a project. Often the approach of taking on a project exposes you to more ideas and techniques than would have been exhibited by just learning from a book. That’s the approach the authors take in “The Rust Programming Language,” and since we are following along, that’s what we will also do. In today’s post, we are going to be building a simple guessing game that tries to have you find a random number between 1and 100. On a quick side note for the first few parts of this series, I will be following along with “The Rust Programming Language.” Steve Klabnik and Carol Nichols are the authors of this delightful book which can be found here https://doc.rust-lang.org/book/ or here https://nostarch.com/Rust2018. I strongly encourage you to buy a copy and support the authors. All credit for this source material goes to them. Also, this series of articles documents my experiences learning Rust. Thus I will not be covering all the information discussed in the book. A second note this is the second part of a 51 part series, so if you haven’t read the previous parts of the series here, it is (https://medium.com/@shawnbachlet/boom-or-bust-50-days-to-colorado-gold-rust-day-1-286898dd1dd6. Here is a link to the beginning of the series if you want to start reading from there https://medium.com/@shawnbachlet/boom-or-bust-50-days-to-colorado-gold-rust-day-0-58091dc6d92c.

Setting Up

The initial setup of the project is simple and reminiscent of getting started with Haskell’s Stack tool. It’s only been two days; however, it is becoming abundantly clear why many Haskellers seem to be taking a liking to Rust. To make a new package titled guessing_game we type.

$ cargo new guessing_game --bin

The above line sets us up with a new directory named “guessing_game” that we can change into and get started. We’re also going to need to add,

rand = “0.3.14”

right under the dependencies line in our “Cargo.toml” file. The above line of code will bring in the random number “Crate,” which is just what Cargo/Rust calls a package. These “Crates” could be either binary executables or libraries.

The Code

Once we are all set up the code itself is very easy to understand for anyone that has done a decent amount of programming. I will post the code for the whole project below and then discuss some of the more exciting aspects of it.

extern crate rand;use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);loop {
println!("Please input your guess.");
let mut guess = String::new();io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}

There are four main things of interest in the code above.

  • Pattern Matching
let mut guess = String::new();
  • What looks like the Ord class
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
  • Error handling and IO that looks a lot like monads
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
  • Explicitly declared mutable data
let mut guess = String::new();

Now, I won’t go into a ton of detail about why I find each of these exciting. I will say that each one of the listed items is a common technique in functional programming. I know it’s early but, so far what I am seeing is that Rust is what you would get if you crossed many of the best parts of C++ and Haskell. I’ve got to say. I’m very excited!

--

--