Rust: Getting Started

Rust: Getting Started
Photo by Nate Grant / Unsplash

Rust is an influential systems programming language that offers the performance of low-level languages with the safety and ease of use of high-level languages. You're in the right place if you're new to Rust and eager to dive into systems programming.

In this guide, we'll walk you through the basics of Rust and how to get started with your first program.

Installing Rust

Installing the Rust toolchain is the first step to start programming in Rust. You can easily do this by visiting the official Rust website and following the instructions for your operating system.

Once Rust is installed, you can check if everything is set up correctly by running the following command in your terminal:

$ rustc --version

If you see the version of Rust installed on your machine, congratulations! You're ready to start coding in Rust.

Your First Rust Program

Let's create a simple "Hello, World!" program in Rust. Create a new file named main.rs and add the following code:

fn main() {
    println!("Hello, World!");
}

To compile and run your program, use the following commands:

$ rustc main.rs
$ ./main

You should see the outputHello, World!indicating that your program ran successfully.

The Fibonacci Sequence

Let's dive into a classic example to demonstrate Rust's syntax and features - the Fibonacci sequence. Here's a simple implementation of the Fibonacci sequence in Rust:

fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

fn main() {
    for i in 0..10 {
        println!("Fibonacci({}) = {}", i, fibonacci(i));
    }
}

Save the above code in a file named fibonacci.rs and run it using the same commands as before:

$ rustc fibonacci.rs
$ ./fibonacci

You'll see the Fibonacci sequence up to the 10th number displayed on your terminal.

We Covered

Now, you took the first steps into systems programming with Rust! This guide covered the basics of setting up Rust, creating a simple program, and exploring a classic example. Stay tuned for more advanced topics and examples in future blog posts. Happy coding!

Subscribe to Miguel Norberto

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe