Post

Learning Rust EP.1

Learning Rust EP.1

Learning Rust EP.1

Introduction

With the start of the Advent, I decided to kill two birds with one stone and learn Rust. I have been hearing a lot about Rust and how it is a great language for systems programming.

I have been programming in C++ for a lot now, and I am excited to see how Rust compares to C++.

Installation

Being on macOS, the installation is pretty straightforward. I just had to run the following command:

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command downloads and installs the Rust tool chain.

I then proceeded to create a place to store my projects:

1
2
3
4
mkdir ~/projects/rust
cd ~/projects/rust
mkdir hello_world
cd hello_world

And I was ready to write my first Rust program.

Hello World

I created a file called main.rs and wrote the following code:

1
2
3
fn main() {
    println!("Hello, world!");
}

I then compiled the program using the following command:

1
rustc main.rs

And ran the program:

1
./main

And with great joy, I saw the following output:

1
Hello, world!

I was very happy, but I did not mean to stop there.

Guess the Number!

The first thing I did to test out the language, was to create a game in which the computer would generate a random number and the player would have to guess it.

I created a file called guess_the_number.rs and wrote the following code:

1
2
3
4
fn generate_random_number() -> u32 {
    let secret_number = rand::rng().random_range(1..=100);
    secret_number
}

This function generates a random number between 1 and 100 and returns it (u32 is an unsigned 32-bit integer). The syntax 1..=100 is a range that includes both 1 and 100. Pretty neat!

I then wrote the main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    println!("Guess the number!");
    let secret_number = generate_random_number(); // Generate a random number between 1 and 100

    loop {
        println!("Please input your guess.");
        let mut guess = String::new(); // mut = mutable variable

        // Read the input from the user
        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);

        // Compare the user's guess with the secret number
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                // Exit the loop if the user guessed the correct number
                break;
            }
        }
    }
}

I then compiled the program and ran it:

1
2
rustc guess_the_number.rs
./guess_the_number

And I was able to play the game! I was very happy with the result. Wait for the next episode to see what I will do next!

Conclusion

I am very happy with Rust so far. The syntax is very clean, and the language is very powerful. I am excited to see what I will be able to do with it in the future.

Stay tuned for more! Thanks for passing by! 🎄

Federico

This post is licensed under CC BY 4.0 by the author.