Introduction to Rust

Reviewed & published by Brayan K

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — it compiles to native machine code yet guarantees memory safety at compile time, with no garbage collector.

Part of the free Rust course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what Rust is and why it exists, how Cargo builds and runs your code, and you'll have written and run your own first Rust program that prints to the terminal.

What You'll Learn in This Lesson

1️⃣ What is Rust?

Rust is a systems programming language — the kind used to build software where performance and reliability matter most, like operating systems, browsers, and game engines. It first appeared in 2010 and was designed to solve a long-standing tension: languages like C and C++ are blazingly fast but make it easy to introduce memory bugs, while safer languages usually pay for that safety with a garbage collector and slower runtime. Rust gives you both. Its core ideas are:

You'll start exactly where every Rust developer starts: a tiny program that prints text. Read the worked example below first — every line is explained — then run it yourself.

// This is a Rust program. Every Rust program starts running in a function
// called main. The fn keyword declares a function.
fn main() {
    // println! is a MACRO (note the !), not a function. It prints a line of
    // text to the terminal, followed by a newline.
    println!("Hello from Rust!");      // -> Hello from Rust!
    println!("Safe and fast.");        // -> Safe and fast.

    // Rust is statically typed, but it can infer simple types for you.
    // Here the compiler knows total is an integer.
    let total = 2 + 2;
    // {} is a placeholder that gets filled in by the value after the comma.
    println!("2 + 2 = {}", total);     // -> 2 + 2 = 4
}

2️⃣ Why Rust? Safety Without a Garbage Collector

What makes Rust special is when it catches mistakes: at compile time, before the program ever runs. In many languages a bug like using freed memory shows up later as a crash in production. Rust refuses to compile such code. It does this with a system of ownership (which you'll study in depth later) that lets it free memory automatically — no garbage collector required. Read the comments below, then run it.

fn main() {
    // Rust catches mistakes at COMPILE time, before the program ever runs.
    // Variables are immutable by default — this one cannot be changed.
    let language = "Rust";
    let year = 2010; // Rust first appeared publicly in 2010.

    println!("{} is a systems language announced in {}.", language, year);

    // Rust has no garbage collector. Instead, the compiler tracks who "owns"
    // each piece of memory and frees it automatically at the right moment.
    // You get the safety of a managed language with the speed of C.
    let speed = "native machine code";
    let safety = "no null pointers, no data races";
    println!("Compiles to: {}", speed);
    println!("Guarantees: {}", safety);
}

Notice that language and year were declared with plain let and never changed. In Rust, variables are immutable by default — a small rule that prevents a whole class of bugs. You'll learn how to opt into mutability with mut in the next lesson.

Your turn. The program below works once you fill in the three blanks marked ___. Follow the 👉 hints, then run it and compare with the expected output.

// 🎯 YOUR TURN — finish the program, then run it with: cargo run
fn main() {
    // 1) Store your name in a variable. String text uses "double quotes".
    let name = ___;            // 👉 your name, e.g. "Sam"

    // 2) Print a greeting that includes the variable using a {} placeholder.
    println!("Hello, {}! Welcome to Rust.", ___);   // 👉 the variable above

    // 3) Print a simple calculation. Fill in the placeholder for the result.
    let minutes_in_a_day = 24 * 60;
    println!("Minutes in a day: {}", ___);          // 👉 the variable above

    // ✅ Expected output (example):
    //    Hello, Sam! Welcome to Rust.
    //    Minutes in a day: 1440
}

3️⃣ Building and Running with Cargo

Cargo is Rust's build tool and package manager, installed automatically with Rust. Instead of calling the compiler by hand, you let Cargo create a project, fetch dependencies, build your code, and run it. The commands below are the everyday workflow of a Rust developer.

# First, check Rust and Cargo are installed (prints version numbers):
rustc --version
cargo --version

# Create a new project called hello (makes a folder with starter files):
cargo new hello
cd hello

# Build AND run the program in one step. Cargo compiles src/main.rs first:
cargo run

# The new project's default src/main.rs already prints a greeting,
# so the output below is what you'll see:

Mini-Challenge: An "About Me" Report

No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it with cargo run, and check your output against the example in the comments. This is exactly the kind of tiny program that builds real fluency.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Rust Basics

ConceptRust Syntax
Program entry pointfn main() { }
Print a lineprintln!("hi");
Print with a valueprintln!("{}", x);
Declare a variablelet x = 5;
Create a projectcargo new app
Build and runcargo run

🎉 Lesson Complete!

Practice quiz

Where does every runnable Rust program start executing?

  • The start function
  • The first line of the file
  • The fn main() function
  • The run() method

Answer: The fn main() function. Execution begins in fn main(), the program's entry point.

Why is println! written with an exclamation mark?

  • It is a macro
  • It is a regular function
  • It throws an error
  • It marks a constant

Answer: It is a macro. println! is a macro, and macros are invoked with a trailing !.

What does the {} do inside a println! string?

  • Comments out text
  • Starts a code block
  • Escapes a quote
  • Acts as a placeholder for a value

Answer: Acts as a placeholder for a value. {} is a placeholder filled by the value given after the comma.

How does Rust manage memory?

  • With a garbage collector
  • Through ownership rules at compile time
  • Manually with free()
  • It never frees memory

Answer: Through ownership rules at compile time. Rust uses ownership to free memory automatically with no garbage collector.

By default, Rust variables declared with let are:

  • Immutable
  • Mutable
  • Global
  • Constant expressions

Answer: Immutable. Variables are immutable by default; you opt into change with mut.

When does Rust catch many memory bugs?

  • At runtime only
  • Never
  • At compile time
  • During garbage collection

Answer: At compile time. Rust refuses to compile unsafe code, catching bugs before the program runs.

Which command creates a new Cargo project called hello?

  • cargo build hello
  • cargo new hello
  • cargo run hello
  • cargo init --name

Answer: cargo new hello. cargo new hello creates a folder with starter project files.

What does cargo run do?

  • Only downloads dependencies
  • Formats the code
  • Deletes the target folder
  • Builds and runs the program

Answer: Builds and runs the program. cargo run compiles src/main.rs and then runs the resulting program.

What kind of code does Rust compile to?

  • Bytecode for a virtual machine
  • Native machine code
  • JavaScript
  • An interpreted script

Answer: Native machine code. Rust compiles straight to native machine code, so it runs as fast as C.

Which tool is installed alongside Rust to build and manage projects?

  • npm
  • Make
  • Cargo
  • pip

Answer: Cargo. Cargo is Rust's build tool and package manager, installed with Rust.

Continue this course

Frequently asked questions

What exactly is Rust, and what is it used for?

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency. It compiles directly to native machine code (like C and C++) so it is extremely fast, but unlike those languages it guarantees memory safety at compile time without a garbage collector. People use Rust to build operating systems, web browsers (parts of Firefox are written in Rust), command-line tools, game engines, web servers, embedded devices, and WebAssembly modules.

Do I need to know C or C++ before learning Rust?

No. While Rust competes with C and C++ for performance, you do not need to know them first. Some experience with any programming language — variables, functions, loops — will help, but this course starts from the basics. Rust does introduce ideas like ownership and borrowing that are unfamiliar even to experienced programmers, so coming in with an open mind matters more than prior systems-programming experience.

Why does Rust not have a garbage collector?

A garbage collector pauses your program periodically to clean up unused memory, which costs performance and predictability. Rust takes a different approach: the compiler uses a system of ownership and borrowing to figure out exactly when each value is no longer needed and inserts the cleanup automatically at compile time. The result is memory safety with no runtime garbage-collection pauses — you get the speed of manual memory management without the bugs.

How do I install Rust and run my first program?

Install Rust with rustup from rust-lang.org — it gives you the compiler (rustc) and the build tool (cargo). Create a new project with cargo new hello, move into the folder, and run cargo run. Cargo compiles your code in src/main.rs and runs it, printing anything you pass to println! to the terminal. You can confirm the install worked by running rustc --version.