In the rest of this article, we will go over how to set up the basic Rust compiler, rustc, and how to write a basic hello world program on multiple threads!...

5 months ago

Latest Post Kubernetes Cheat Sheet by Tyler Moon

Recently the Rust programming language has been becoming more and more popular. According to the 2019 StackOverflow programming survey Rust is used by only 3.2% of the 87,354 programmers who responded. However, in the most loved section here, 83.5% of developers who use Rust responded that they love working with the language. This prompted me to look into the language, seeing how I had no prior experience, and see why all the Rust programmers liked it so much. In the rest of this article, we will go over how to set up the basic Rust compiler, rustc, and how to write a basic hello world program on multiple threads!

Prerequisites

Note: These instructions are specific to Linux (or MacOS) but there is a Windows version of everything covered

Installing Rustc

The primary way to install Rust is by using an installer and version management tool called Rustup. To download and install Rustup use the following curl command piped to sh.

curl https://sh.rustup.rs -sSf | sh

Take the default install option unless you know of a specific reason to use a custom install. Then run the following to set the Rust commands to your PATH environment variable

source $HOME/.cargo.env

Now to verify that the Rustc compiler is working run the following version command

rustc --version

Writing a Hello World Rust Program

In an empty directory create a new file called HelloWorld.rs and add the following Rust code to it

// HelloWorld.rs

// Import threading packages
use std::thread;
use std::time::Duration;

// Main function
fn main() {
    // Create a new thread
    let handle = thread::spawn(|| {
        // Inside of a closure count from 1 to 10 and print the message
        for i in 1..10 {
            println!("Hello World! {} from spawned thread!", i);
            thread:: sleep(Duration::from_millis(1)); // Wait for a milisecond
        }
    });

    // On the main thread count from 1 to 5 at the same time
    for i in 1..5 {
        println!("Hello World! {} from the main thread!", i);
        thread::sleep(Duration::from_millis(1));
    }

    // This holds the main thread until the spawned thread has completed
    handle.join().unwrap();
}

Open a terminal to the directory you just created and run the following to compile your program

rustc HelloWorld.rs

The compiler should spit out a HelloWorld executable which you can run by the following command

./HelloWorld

If it worked you should see an output like the following

Hello World! 1 from the main thread!
Hello World! 1 from spawned thread!
Hello World! 2 from the main thread!
Hello World! 2 from spawned thread!
Hello World! 3 from the main thread!
Hello World! 3 from spawned thread!
Hello World! 4 from the main thread!
Hello World! 4 from spawned thread!
Hello World! 5 from spawned thread!
Hello World! 6 from spawned thread!
Hello World! 7 from spawned thread!
Hello World! 8 from spawned thread!
Hello World! 9 from spawned thread!

And there you go, a hello world Rust program!

Summary

This article explains the very basics of writing Rust code. From installing the compiler, through a basic Hello World program. After my first few days of reading the documentation and going through various tutorials, I am very impressed by Rust. There are a lot of tools and documentation that are very useful. I'll be writing more articles on more advanced Rust topics soon.

Tyler Moon

Published 5 months ago

Comments?

Leave us your opinion.