Writing Your First Rust code : A Beginner’s guide

Jolade OkunladeJolade Okunlade
5 min read

Rust! You’ve heard the buzz: “performance,” “safety,” “concurrency without data races.” It’s a language that’s gaining serious attention for everything from Embedded development, WebAssembly (Wasm), Blockchain smart contracts (e.g., Arbitrum Stylus, Solana), CLI tools and even Operating systems.

But how do you get started?

Fear not! I’ll walk you through setting up your environment and writing your very first Rust program. It’s simpler than you might think.

  1. Installing rust

Before we write any line of code, we need to install the Rust toolchain. The easiest way to do this is with rustup the official Rust installer.

For Windows, macOS, and Linux:

. Open your terminal (PowerShell or Command Prompt on Windows, Terminal on macOS/Linux) and run this commands.

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

Windows users: If you don’t have curl, you might need to download rustup-init.exe directly from the Rustup website and run it.

Once the installation is complete, close your terminal and open a new one. This ensures your shell picks up the new environment variables.

You’ll also get:
rustc: The compiler
cargo: Package manager, build system, task runner
rust-std: Standard library

2. Verify the installation to check whether you have Rust installed correctly, enter:

rustc --version
cargo --version

You should see the version number, commit hash, and commit date for the latest stable version that has been released, in the following format:

rustc x.y.z (abcabcabc yyyy-mm-dd)

3. Build your First Project with Cargo: “Hello, Rust!”

Cargo is Rust’s build system and package manager. Rust uses cargo to manage projects, from creating new projects to compiling your code and managing dependencies. (Just like create react app).

Navigate to a directory where you want to create your project. For example, C:\Projects on Windows or ~/projects on Linux/macOS.

Create a new Rust project called hello_rust:

cargo new hello_rust

This command does a few things:

  • It Creates a new directory named hello_rust.

  • Inside hello_rust, it creates a src directory.

  • Inside src, it puts a main.rs file (this is where our code goes).

  • It creates a Cargo.toml file in the root hello_rust directory (this is your project's configuration file).

4. Navigate into your new project directory:

cd hello_rust

Now, let’s open src/main.rs in your favorite text editor (VS Code is highly recommended with the Rust Analyzer extension). You'll find this code already there:

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

Let’s break this down:

  • fn main(): Declares a function named main. In Rust, main is a special function: it's the entry point of every executable Rust program. The fn keyword indicates it's a function, and the () indicate it takes no arguments.

  • {}: Curly braces define the body of the function. All the instructions within main go inside these braces.

  • println!: This is a Rust macro, not a regular function. You can tell it's a macro by the ! at the end. Macros are powerful code-generating tools. println! prints text to the console.

  • ("Hello, yourname!"): This is the string literal that println! will print.

It’s a simple program that just prints “Hello, yourname!” to your console (printing your name is exciting right?).

5. Compile and run!

With your code ready, let’s see it in action. In your terminal, while still inside the hello_rust directory type:

cargo run

You’ll see output similar to this:

Compiling hello_rust v0.1.0 (C:\dev\escrow\hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.5s
 Running `target\debug\hello_rust.exe`
Hello, yourname!

What happened here?

  • cargo run is a convenient command that first compiles your code and then runs the resulting executable.

  • Compiling hello_rust...: cargo invoked the Rust compiler (rustc).

  • Finished dev [unoptimized + debuginfo] target(s): The compiler successfully translated your human-readable Rust code into machine-executable binary code. By default, cargo builds a "debug" version, which is faster to compile but not optimized for speed.

  • Running \target\debug\hello_rust.exe: cargo then executed the compiled program.

  • Hello, yourname!: And there it is! Your program's output.

6. Modify and Experiment

fn main() {
    // Variables are immutable by default
    let name = "Rustacean";

    // String interpolation with println!
    println!("Hello, {}!", name);

    // Mutable variables require the 'mut' keyword
    let mut counter = 5;
    println!("Counting down: {}", counter);

    // While loop with mutable state
    while counter > 0 {
        println!("{}...", counter);
        counter -= 1;
    }

    println!("Blast off!");

    // Function call
    let result = add(10, 32);
    println!("10 + 32 = {}", result);
}

// Function definition with types
fn add(a: i32, b: i32) -> i32 {
    // Expressions don't need semicolons
    // This is an implicit return
    a + b
}

Replace your main.rs content with this code and run it with cargo run.

Key rust concepts :

  • Variables are immutable by default (let name)

  • Use mut keyword for mutable variables (let mut counter)

  • Rust is statically typed but uses type inference

  • Function parameters require explicit types (a: i32)

  • Return types are specified after -> (-> i32)

  • Expressions return values (like a + b)

  • No semicolon means it’s a return value.

Congratulations!

You’ve just written, compiled, and run your first Rust program. You’ve installed the toolchain, created a new project with cargo, understood the basic structure of a Rust program, and seen it execute. This is the basic building block for all your future Rust projects. Take a moment to appreciate this first step. You’re on your way to mastering a truly remarkable language!

🦀 Welcome to the Rustacean family!

What’s Next?

From here, the world of Rust opens up: variables, control flow, data structures, ownership, borrowing, and so much more.

Happy coding in Rust!

1
Subscribe to my newsletter

Read articles from Jolade Okunlade directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jolade Okunlade
Jolade Okunlade