Learning rust is fun! But that doesn't mean it's easy. Let's jump straight into the deep end to learn.
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:
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