In many programming languages, a mistake in how a program uses the computer's memory (where a running program keeps its data) only appears when the program is already running, sometimes on a user's laptop. Rust catches it earlier. Rust is a compiled language: before a program may run, a program called the compiler checks all of your code, including how the code uses memory. The result is that memory stays safe without a garbage collector, the memory-cleaning program in other languages.
Rust works like Rusdi's Pancake Stall before it opens. Rusdi reads every work note for the evening, line by line. Sometimes a note makes no sense: for example, an order slip has already gone to the kitchen, but the cashier still wants to use it. Then the stall does not open until the note is fixed. The notes also say when each table gets cleared, so nobody has to walk around clearing tables during the busy hours. In this picture, the work notes are your Rust code. The check before opening is the compiler. The helper who walks around, and whom nobody needs, is the garbage collector.
Your first Rust program
Throughout this Rust track we build one program that keeps growing: the till (the cash register program) of Rusdi's Pancake Stall. The menu is Sweet Pancake at $32, Egg Pancake at $38, and Iced Tea at $5. Every price is stored as a whole number of dollars. The kitchen has 250 eggs in stock, and one Egg Pancake uses 3 of them.
A Rust program is made with Cargo, Rust's own tool for creating, compiling and running projects (a project is one folder that holds a program's code). You type Cargo commands in a terminal, the window where you give the computer commands as text. The first command creates a new project. The line that starts with $ is the command you type (without the $), and the line after it is Cargo's answer:
$ cargo new stall
Creating binary (application) `stall` package
Cargo makes a folder stall that holds two files: Cargo.toml (the project's name and version) and src/main.rs (the code). Put this code in src/main.rs:
fn main() {
println!("Rusdi's Pancake Stall");
println!("Sweet Pancake: {}", 32);
println!("2 portions: {}", 32 * 2);
}
Then run the program with cargo run, from inside the stall folder:
$ cargo run
Compiling stall v0.1.0 (/home/you/stall)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.47s
Running `target/debug/stall`
Rusdi's Pancake Stall
Sweet Pancake: 32
2 portions: 64
fn main() { ... }is where the program starts.fnmeans function, and Rust always runs the body ofmainfrom top to bottom.println!writes one line to the terminal. The exclamation mark!marks a macro: a short command that the compiler rewrites into full code.{}inside the text is an empty slot. The value after the comma fills it, so"Sweet Pancake: {}", 32prints asSweet Pancake: 32.32 * 2is calculated first. What gets printed is the result,64.- Text goes between double quotes
"...". Each command ends with a semicolon;, and the lines inside the curly braces start with 4 spaces. - In the result of
cargo run, the first three lines after the command are Cargo's report: compiling, finished, then runningtarget/debug/stall. The three lines after them are your program's output.
On your own computer, install Rust with rustup from rustup.rs, then check it with the command rustc --version. The result looks like rustc 1.100.0 (0ef1bc386 2026-11-09). rustc is the name of the Rust compiler, and Cargo calls it for you. In BelajarWeb.dev exercises, the Run button uses a Rust simulator in the browser, so you don't need to install anything yet.
Compiled: checked first, made into one program file
You need to know the difference between two times. Compile time is when the compiler checks your code and translates it into a program. Runtime is when that translated program actually runs. Three Cargo commands to know:
cargo run: compiles, then runs the program right away. You use this command most while learning.cargo build: compiles only. The result is the filetarget/debug/stall. You can run that file with./target/debug/stallas many times as you like, without compiling again.cargo build --release: compiles with full optimisation. The result is the filetarget/release/stall. It takes longer to compile, but the program runs faster. This is the file you put on a server (a computer that serves users).
The compiled file is called a binary: machine code that a computer's processor runs directly. That is why the computer that runs the binary does not need Rust installed. If you run cargo run again without changing the code, Cargo does not compile again: the report line becomes Finished ... in 0.00s, and the program runs right away.
The compiler checks first
Now remove the exclamation mark on line 3 of src/main.rs, so the line becomes println("Sweet Pancake: {}", 32);. Before you look at the result of cargo run, guess. Line 2 prints Rusdi's Pancake Stall, and you did not change it at all. Does that text still print? Here is the result:
$ cargo run
Compiling stall v0.1.0 (/home/you/stall)
error[E0423]: cannot find function `println` in this scope
--> src/main.rs:3:5
|
3 | println("Sweet Pancake: {}", 32);
| ^^^^^^^ not found in this scope
|
= note: a macro named `println` exists in another namespace
help: use `!` to invoke the macro
|
3 | println!("Sweet Pancake: {}", 32);
| +
For more information about this error, try `rustc --explain E0423`.
error: could not compile `stall` (bin "stall") due to 1 previous error
error[E0423]is the error code. The message next to it means there is no function namedprintln: without!, the compiler looks for a function, butprintlnis a macro. The commandrustc --explain E0423explains the error in full.src/main.rs:3:5means the filesrc/main.rs, line 3, column 5. The^^^^^^^marks point at the wrong part.help:suggests a fix, with the code included. The+mark shows the character to add, which is!.- The answer to the guess: no. Not a single line prints, not even
Rusdi's Pancake Stallfrom line 2, whose code is correct. The compile failed, so no program ran.
Memory safe, without a garbage collector
A program keeps its data (order text, prices) in memory. Data that is no longer used has to be thrown away so the memory can be used again. A programming language handles this in one of three ways:
- A garbage collector: a cleaning program that runs along with your program and from time to time throws away data that is no longer used. JavaScript, Python, Go and Java use one. This way is safe, but it is extra work while the program runs.
- By hand, as in C and C++: the programmer writes the code that throws data away. This way is fast, but a small mistake can be fatal, such as using data that was already thrown away. Microsoft has reported that about 70% of the security holes it handles each year come from memory bugs like this.
- Rust: every piece of data has one owner, the variable that holds it. The rules about who may use which data are checked at compile time, by a part of the compiler called the borrow checker (to borrow: to use data without becoming its owner). At the end of the curly-brace block where the owner was made, the compiler itself inserts the code that throws the data away. The result is as safe as a language with a garbage collector, and as fast as C/C++.
The code below shows the stall's order slip: it has gone to the kitchen, but the cashier still uses it. The first block is the code, the second is the compiler's error:
fn main() {
let order = String::from("Sweet Pancake");
let kitchen = order;
println!("Cooking: {}", kitchen);
println!("Order: {}", order);
}
error[E0382]: borrow of moved value: `order`
--> src/main.rs:5:27
|
2 | let order = String::from("Sweet Pancake");
| ----- move occurs because `order` has type `String`, which does not implement the `Copy` trait
3 | let kitchen = order;
| ----- value moved here
4 | println!("Cooking: {}", kitchen);
5 | println!("Order: {}", order);
| ^^^^^ value borrowed here after move
let makes a variable (a named box holding a value). String::from(...) makes text that can be moved to another variable. On line 2, the owner of the text is order. Line 3 moves that text into kitchen, so order no longer holds it. Line 5 still tries to use order. The error message, "borrow of moved value", says that the code borrows (uses) a value that was already moved. The borrow checker refuses this code at compile time, not while the program runs. That is why even Cooking: Sweet Pancake never gets printed. The full error message also has a help: suggestion: use order.clone(), which makes a copy of the text. The full rules about owners of data come later, in the lessons Ownership and Borrowing & References.
🎯 What Rust is used for
- Infrastructure and systems: the Linux kernel (the core of the Linux operating system) has accepted Rust code since version 6.1. Cloudflare wrote Pingora, its proxy (a server that sits in between), in Rust. Discord moved one of its services from Go to Rust because of pauses caused by the garbage collector.
- Web tooling (tools for web developers): SWC (a Babel replacement), Turbopack, Rspack and Biome are written in Rust. That is why they are fast.
- In the browser and on the desktop: Rust can be compiled to WebAssembly, a code format that browsers can run. Tauri builds desktop apps that are lighter than Electron apps.
Rust 1.0 was released in 2015, and the Rust Foundation now manages it. For a one-time script (a small program) or an ordinary web page, JavaScript or Python is usually faster to write.
⚠️ Small typos that stop the compile
- Forgetting
!onprintln. The error iscannot find function `println` in this scope.println, and alsoformatandvec, which you will meet later, are all macros, so they are always written with!. - Forgetting the semicolon.
println!("...")with no;at the end, and anotherprintln!on the next line, gives the errorexpected `;`, found `println`(the compiler expected;but foundprintln). The error points at the next line. - Text in single quotes.
'Pancake Stall'is not text in Rust. Single quotes are only for one character. Text is always written with"...". - Forgetting
{}.println!("Sweet Pancake:", 32)is refused with the errorargument never used(the value is never used), because there is no empty slot for the number.
TL;DR: Rust is a compiled language: before a program runs, the compiler checks all of the code and translates it into one program file. Rust is memory safe (memory: where a running program keeps its data) without a garbage collector (the memory-cleaning program in other languages). Cargo, Rust's own tool, makes a project (a folder of program code) with cargo new stall, the code is in src/main.rs, and cargo run compiles it and then runs it. A program starts at fn main(). println! prints one line, and it is written with ! because it is a macro (a special kind of command). {} is filled by the value after the comma. One error at compile time (while compiling) means not a single line runs. The borrow checker, a part of the compiler, makes sure at compile time that data is not used after it has been moved to another variable. So memory bugs are caught before the program runs. The compiler also inserts the code that throws data away, so no garbage collector is needed while the program runs (runtime).
⚡ A program starts at main
Write:
fn main() {
println!("Rusdi's Pancake Stall");
println!("Sweet Pancake: {}", 32);
println!("2 portions: {}", 32 * 2);
}
Meaning: The body of fn main() runs from top to bottom. println! prints one line. The ! is required, because println is a macro, a special kind of command in Rust. {} is filled by the value after the comma. 32 * 2 is calculated first, so what prints is 2 portions: 64.
⚡ One error, zero lines
Guess first: in the code from the previous step, the ! on line 3 is removed. Line 2 prints Rusdi's Pancake Stall, and you did not change it. Does that text still print?
No. Before a program runs, the compiler (a program that checks and translates your code) reads all of the code. The compile fails with error[E0423], so no program runs, and line 2 does not print either.
Meaning: The compiler checks all of the code at compile time (while compiling), before runtime (while the program runs). If there is even one error, not a single line runs.
⚡ Safe without a garbage collector
Write:
let order = String::from("Sweet Pancake");
let kitchen = order;
println!("Order: {}", order); // error[E0382]
Meaning: Line 1 makes text with String::from(...). Line 2 moves that text from order into kitchen, so order no longer holds it. Line 3 still uses order. The borrow checker, the part of the compiler that checks how data is used, refuses this code at compile time, not while the program runs. In C and C++, a mistake like this is not refused at compile time. Rust is memory safe (memory: where a running program keeps its data) without a garbage collector (the memory-cleaning program in other languages).