Learn What is Rust?

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…

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

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:

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

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:

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

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

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).

What you will learn