AheadKit

Free chapter — read online

Rust for Everyone: Chapter 1, free to read

This is a free chapter of Rust for Everyone: Chapter 1 — “Introduction” — all 8 pages (pages 5–12 of the full 307-page book), exactly as they appear in the book. No edits, no teaser cuts.

Chapter 1: Introduction

Version: Rust 1.99 · Edition 2024

What Is Rust?

Rust is a programming language for building fast, reliable software. It was started at Mozilla in 2006, became stable with version 1.0 in 2015, and is now developed in the open by the Rust Foundation and a large community of contributors.

Two words come up again and again when people describe Rust: speed and safety.

Speed means Rust programs run as fast as programs written in C or C++. Your code is turned into machine code, the direct instructions your computer's processor understands. There is no interpreter in between and no background worker slowing things down.

Safety means Rust stops a whole class of common bugs before your program ever runs. In many languages, mistakes with memory — using data after it was freed, reading memory you do not own, two parts of a program changing the same data at once — cause crashes and security holes. Rust's compiler checks for these problems while you build the program, not after a user hits them.

No Garbage Collector — in Plain Words

Many languages clean up memory for you with a garbage collector: a background process that watches your program, finds memory that is no longer used, and frees it. That is convenient, but the collector runs on its own schedule. It can pause your program at awkward moments, and it uses memory and processor time of its own.

Rust has no garbage collector. Instead, Rust follows a set of rules called ownership, which you will meet properly in Chapter 6. The short version: the compiler works out, ahead of time, exactly when each piece of data is no longer needed, and puts the cleanup right there in the machine code. Nothing runs in the background, and nothing pauses your program, so you get tidy memory at full speed.

That trade shapes everything else in the language. You follow the compiler's rules while you write the code. In return, the finished program is fast and a large group of bugs simply cannot happen.

NOTE The Rust compiler will feel strict at first. Every Rust programmer goes through a phase of arguing with it. That phase passes, and the checks that annoyed you start to feel like a careful friend reviewing your work before anyone else sees it.

Where Rust Is Used

Rust started as a systems language — the kind used for operating systems, browsers, and tools that sit close to the hardware. It has spread well beyond that:

Area What Rust does there

Web browsers Parts of Firefox are written in Rust, including pieces of its rendering engine

Command-line tools Fast everyday tools such as ripgrep (text search) and bat (a modern cat )

Cloud and infrastructure Services and tools at large scale, where speed saves real money on servers

Embedded devices Small chips in sensors, controllers, and devices with very little memory

Web back ends Servers and APIs, often where reliability matters most

Games and media Game engines, audio and video tools, and performance-heavy parts of larger apps

Big companies use Rust in production. Microsoft has used Rust for parts of Windows and Azure and has said publicly that it wants new systems code written in memory-safe languages like Rust. Google uses Rust in Android and Chrome. Amazon uses Rust at AWS, including in services like Firecracker, which starts tiny virtual machines for serverless computing. Meta and Dropbox have also used Rust for performance-sensitive back-end work.

You do not need to work at any of those places for Rust to be useful. The same qualities that make it good for a cloud giant — speed, small size, no surprise pauses — make it good for a tool you write for yourself on a laptop in Dhaka.

Why Learn Rust?

A few honest reasons:

  • Jobs and demand. Companies that run large systems care about server costs and reliability. Rust skills are still rare compared with the number of teams that want them, and rare skills get noticed on a CV.
  • Programmers rate it highly. In Stack Overflow's 2024 developer survey, Rust was the most admired language for the second year running — the one the highest share of its users want to keep using. People who learn Rust tend to stay with it.
  • Performance without fear. You get speed close to C, but the compiler catches memory mistakes that in C would become crashes or security bugs.
  • It makes you better at every language. Rust forces you to think clearly about who owns data and how long it lives. That thinking transfers. Programmers often say their code in other languages improved after learning Rust.
  • One language, many jobs. The same language can write a tiny program for a microcontroller, a fast command-line tool, and a web server. Learning it once covers a lot of ground. PRO TIP Fashion is a weak reason to learn a language. The stronger reasons are practical: programs that run fast and break rarely, plus an ownership habit that improves your code in every other language you write.

The Three Tools You Will Meet

You only need three tools to work in Rust. They come together, and you will use them every day:

Tool What it does You use it to

rustc The compiler Turn Rust source code into a program you can run

Cargo The build tool and package Create projects, build them, run them, test them, and download manager libraries

rustup The installer and version manager Install Rust and keep it up to date

Think of it this way. rustc does the actual compiling, like a cook doing the cooking. Cargo runs the whole kitchen: it calls the compiler for you, in the right order, with the right settings, and fetches ingredients (libraries, called crates) when a recipe needs them. rustup is the person who sets up the kitchen and replaces old tools with new ones.

In daily work you will mostly type cargo commands. rustc matters in this first chapter because seeing the compiler work directly, once, makes everything Cargo does later much less mysterious.

Installing Rust

Rust is installed with rustup . The steps:

  • Go to the official site, rust-lang.org, and follow the install instructions for your system. On Windows, the site gives you a small rustup-init.exe file to run. On macOS and Linux, it gives you one terminal command that downloads and runs the installer.
  • Accept the default options. The installer puts rustc , Cargo , and rustup on your machine and adds them to your PATH, so your terminal can find them.
  • Close and reopen your terminal, so it picks up the change.

That is all the setup there is. One installer, no separate downloads, no licence, no fee. Rust is free and open source.

Checking Your Install

Open a terminal and ask both tools for their versions:

rustc --version
cargo --version

Output (from the machine this book was written on):

rustc 1.99.1 (58cf8f9c3 2026-10-13)
cargo 1.99.1 (5f94df478 2026-08-27)

Your numbers may be a little newer — that is fine, and good. rustup update is the command that brings you up to date whenever you want the latest stable release. If either command says "not found" instead, the install did not finish or the terminal was not reopened. Reopen the terminal first; that fixes it for most people.

GOTCHA Always download Rust from rust-lang.org or through your system's official package routes that point back to it. Random "Rust installer" downloads from other sites are a classic way to pick up malware — and there is never a reason to use one, because the real thing is free.

Your First Program, the Direct Way

Every programming book starts with a tiny program that prints a line of text. We will do it twice: first by handing a single file straight to rustc , so you see the raw process, then the Cargo way, which is how real projects work.

Create a file named hello.rs with this exact content:

fn main() {
println!("I am learning Rust!");
}

Save it, then compile and run it. On macOS or Linux:

rustc hello.rs
./hello

On Windows, the compiler produces hello.exe , and you run it with .\hello.exe . Either way, you see:

Output:

I am learning Rust!

Two separate things happened. rustc hello.rs compiled the file: it read your text, checked it, and produced a finished program file ( hello or hello.exe ). Running ./hello was a separate step that executed that finished program. Writing, compiling, and running are three different moments. When a program fails to compile, it never runs at all — the error you get is from the checking stage, and Chapter 2 onwards you will read a lot of those messages. They are helpful once you know how to read them.

The Hello Program, Line by Line

Code What it means

fn main() { Starts a function named main . Every Rust program begins by running main . fn is short for function.

println!("I am Prints one line of text. The ! says println! is a macro, a special kind of code you will learning Rust!"); meet later. The text goes in double quotes, and the line ends with a semicolon.

}

Ends the main function. Everything a function does sits between its curly braces.

Three details to lock in now:

  • Indentation is four spaces by convention. Rust does not force it, but every Rust programmer expects it, and the automatic formatter ( cargo fmt ) uses it.
  • Most lines end with a semicolon. Leaving one out is the single most common first-day error — you will see exactly what it looks like in a moment.
  • The file name ends in .rs . That is how everyone, and every tool, knows a file holds Rust code.

Your First Program, the Cargo Way

Real Rust work happens inside projects, and Cargo manages them. Let Cargo create one:

cargo new hello_cargo

Output:

Creating binary (application) `hello_cargo` package

Cargo makes a folder named hello_cargo with a small, sensible layout:

File or folder What it is

Cargo.toml The project's settings file: its name, version, and the crates it depends on

src/main.rs Your source code. Every Cargo project starts here

target/ Created when you build. Holds the compiled program and working files

Open src/main.rs . Cargo has already written a starter program for you:

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

It is the same shape as your hello.rs : a main function and one println! . Now stand inside the project folder and run it the Cargo way:

cargo run

Output:

Compiling hello_cargo v0.1.0
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.46s
Running `target/debug/hello_cargo`
Hello, world!

(The full folder path after the project name, and the exact build time, will differ on your machine.)

You will see this output hundreds of times, so read it properly once, now: Cargo compiled the project, reported that it finished, then ran the program it built, and the program printed Hello, world! . One command did all three steps. Run cargo run again and the first two lines change: with nothing edited, Cargo skips compiling and goes almost straight to running. It rebuilds only what changed. That is the kitchen manager earning his keep.

From here on, this book uses Cargo projects and cargo run for anything beyond a one-file experiment.

How Rust turns your text into a running program:

The Classic First Mistake: a Missing Semicolon

Sooner or later — usually on day one — you will forget a semicolon. Let us do it on purpose now, while it is cheap. Change the hello program to this broken version, with a second line added and no semicolon on the first println! :

fn main() {
println!("I am learning Rust!")
println!("Second line");
}

Compile it, and the compiler answers (shortened):

error: expected `;`, found `println`
--> ex02-missing-semicolon.rs:2:36
|
2 |     println!("I am learning Rust!")
|                                    ^ help: add `;` here
3 |     println!("Second line");
|     ------- unexpected token

Do not skim past error messages like this — they are some of the best teaching in Rust. This one tells you:

  • What went wrong, in the first line: it expected a ; and found println instead.
  • Where, with a file name, line, and column: line 2, column 36 — right at the end of the broken line.
  • A suggested fix, marked with help: — here, literally "add ; here".

Add the semicolon back, recompile, and the program runs. Get into the habit of reading the first line and the help: line of every error. Between them, they solve most beginner problems without any searching.

PRO TIP Compiler errors are not the program failing you. They are the program refusing to be built wrong. A Rust error message that stops you for thirty seconds today is a crash — or a security bug — that never reaches a user tomorrow.

What You Have Now

You can install Rust, check its version, compile a file directly with rustc , and create and run a Cargo project. You have also read your first real compiler error and fixed it. That is a complete working setup — everything else in this book is built on these few commands.

Try It Yourself

  • Write a program in a file named hello_name.rs that prints two lines: Hello, my name is Salam! and I

live in Dhaka. (use your own name and city). Compile it with rustc and run it.

Answer

fn main() {
println!("Hello, my name is Salam!");
println!("I live in Dhaka.");
}

Output:

Hello, my name is Salam!
I live in Dhaka.

Each `println!` prints one line, in the order the lines appear in `main`.

  • Create a new Cargo project named three_lines , and change its src/main.rs so the program prints

Line one , Line two , and Line three , each on its own line. Run it with cargo run .

Answer The `src/main.rs` file should contain:

fn main() {
println!("Line one");
println!("Line two");
println!("Line three");
}

Output (the `Compiling` and `Finished` lines from Cargo come first):

Line one
Line two
Line three
  • Find the mistake in this program, fix it, and run the fixed version:
fn main() {
println!("This works now!")
println!("Second line");
}

Answer The first `println!` line is missing its semicolon. The compiler answers (shortened):

error: expected `;`, found `println`

Add the `;` at the end of that line:

fn main() {
println!("This works now!");
println!("Second line");
}

Output:

This works now!
Second line

Next: variables — how a Rust program remembers values, and why Rust will not let you change most of them unless you say so up front.

That was Chapter 1. The book has 23 more chapters.

The full book: 307 pages, 24 chapters and 9 step-by-step projects, for $2.99 — or all 11 programming books in the bundle for $12.99.

Added to cart