Learning rust

Learning rust is fun! But that doesn't mean it's easy. Let's jump straight into the deep end to learn.

A guessing game

Let's make a guessing game. The user will guess a number between 1-100 and we will say if its higher or lower.

First, break down the steps, into components that we can individually Google or just follow along:

  1. Generate a random number.
  2. Get input from standard in.
  3. Compare this input to the random number generated.
  4. Say if it's higher or lower or equal. If it's equal, exit; if it's not, go back to #2.
  5. Congratulate user.

Before we can even start with the project, we should probably install rust. Visit rustup.rs and follow the instructions. On Windows, read the top part carefully. Now open up your in a folder where you would like to keep your rust projects. Run cargo new guessing_game in the terminal, then open the folder in your favorite text editor, and we can begin.

Let's start with the first step. We need to generate a random number. We can do this with the rand crate. rand is a crate that provides random number generators. We can use it by adding it to our Cargo.toml file.

In ./Cargo.toml:

[dependencies]
rand = "0.8.5"
    

Now we can use the crate in our code. In ./src/main.rs:

use rand::Rng; // This is what