April 15, 20256 min

Getting Started with Rust: A Guide for Beginners

M
Mayorana

Rust has been gaining significant traction among developers for its focus on performance, memory safety, and concurrency. If you're new to Rust, this guide will help you get started with the basics.

rustup install toolchain via sh.rustup.rs rustc hello.rs compile a single file ./hello to run cargo new scaffolds project Cargo.toml + src/ cargo build/run manages deps and builds for you rustc works for one file — cargo is the everyday workflow for real projects

Setting Up Your Environment

First, you'll need to install Rust on your system. The easiest way is to use rustup, the Rust toolchain installer:

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

This command will download a script and start the installation process. Follow the instructions on screen to complete the installation.

Your First Rust Program

Let's create a simple "Hello, World!" program. Create a new file called hello.rs with the following content:

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

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

rustc hello.rs
./hello

Understanding Cargo

Cargo is Rust's build system and package manager. It handles many tasks such as building your code, downloading libraries, and building those libraries.

To create a new project with Cargo:

cargo new hello_cargo
cd hello_cargo

This creates a new directory called hello_cargo with the following structure:

hello_cargo/
├── Cargo.toml
└── src/
    └── main.rs

The Cargo.toml file contains metadata about your project and its dependencies. The src/main.rs file contains your application code.

To build and run your project:

cargo build   # Compile the project
cargo run     # Compile and run the project

Key Concepts in Rust

Variables and Mutability

By default, variables in Rust are immutable:

let x = 5;
// x = 6; // This would cause an error

To make a variable mutable, use the mut keyword:

let mut y = 5;
y = 6; // This works fine

Ownership

Ownership is Rust's most unique feature and enables memory safety without garbage collection. The main rules are:

  1. Each value in Rust has a variable that's its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.
fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // s1 is moved to s2, s1 is no longer valid
    
    // println!("{}", s1); // This would cause an error
    println!("{}", s2); // This works fine
}

Nothing was copied here. s1 and s2 are two stack slots, but there is only ever one heap buffer and one owner of it — which is why the compiler retires s1 the moment it hands the buffer to s2:

let s1 = String::from("hello"); let s2 = s1; stack heap s1 moved out — using it is a compile error s2 ptr · len 5 · cap 5 "hello" a single allocation the move never copies it owns } // end of main the one owner goes out of scope the buffer is freed — exactly once no double free, no garbage collector Rule 2 (one owner) and rule 3 (drop at end of scope) are what remove free() from your code

Next Steps

Now that you have the basics, try building a small project to practice your skills. The Rust documentation is an excellent resource for learning more:

Happy coding with Rust!