Rust: why memory bugs disappear before the program runs
Rust catches memory bugs at compile time. This post explains ownership through a real C example, with commands and error messages you can run yourself.
The running program suddenly dies
You hit run on a small program, see it print a few lines, then it vanishes. On Linux, the terminal just adds one line: Segmentation fault (core dumped). At that moment the program touched memory it wasn’t allowed to: memory it was never given, memory already returned, or memory belonging to someone else. With C and C++, the compiler isn’t required to report this. It lets the program run, and you only find out when it crashes or when your data has already been overwritten.
Rust does exactly that part right: it checks memory at compile time, before there’s a program to run. Write something wrong and the compiler stops and points to the exact line.
One array overflow in C
Say you have an array of 5 integers and you write to the 6th element, index 5. The following C compiles fine:
int a[5];
a[5] = 42;
With a current stable gcc, the command gcc -Wall -o thu thu.c prints no warning. When it runs, the program may not crash right away. It writes 42 into the memory cell right after array a. If that cell is another variable, that variable changes value without anyone naming it. If it’s the function’s return address, the program jumps somewhere with no code and crashes. Same line of code, two different outcomes depending on how variables are laid out in memory.
The equivalent Rust doesn’t compile:
let mut a = [0; 5];
a[5] = 42;
You can check this yourself: install Rust via rustup (the official page is rustup.rs), run cargo new thu-nghiem, paste the snippet into src/main.rs, then run cargo build. The compiler prints:
error: this operation will panic at runtime
--> src/main.rs:2:5
|
2 | a[5] = 42;
| ^^^^^^ index out of bounds: the length is 5 but the index is 5
Note the phrase will panic at runtime: the index is a constant so the compiler knows it up front and blocks it right there. If the index comes from a variable, say let i = 5; a[i] = 42;, Rust compiles it, but at runtime it stops right on that line with an index out of bounds message instead of scribbling into the neighbouring cell. Both paths forbid writing outside the array.
Why Rust can block it
Rust lays down a rule called ownership: each piece of data has exactly one variable responsible for it, and when that variable goes out of scope, the memory is returned. There’s no garbage collector running in the background like Java or Go. The compiler tracks who holds the data, who is borrowing to read, who is borrowing to write. At any one moment, either one writer or many readers, never reading and writing at once.
I’ll use just one comparison: it’s like a notebook that lets only one person hold the pen, while anyone else who wants to look has to stand and read. The comparison stops there because memory has no guard: it all lives in the compiler’s rules.
The cost: you have to learn borrow and lifetime, and sometimes rearrange your code so the compiler accepts it. In return, memory needs no garbage collector, so there are no pauses for cleanup while the program runs. For applications that need steady response times, that’s the reason to pick Rust over Go.
The diagram below shows the check sits before the program is produced, and the two branches when it refuses.
graph LR
A["Rust source (.rs)"] --> B["rustc checks types and ownership"]
B --> C{"Valid?"}
C -->|"No"| D["Report errors, emit no machine code"]
C -->|"Yes"| E["Emit machine code"]
E --> F["Run the program"]
Where Rust already is
There are places you use Rust without knowing. Firefox uses Rust in its CSS engine component called Stylo, and in its decompression library. The Linux kernel has driver modules written in Rust since version 6.1. Verify it by downloading the source of those two projects and looking for .rs files; you don’t have to take my word for it.
That’s why Rust shows up a lot in projects that need near-C speed but don’t want to pay with memory bugs. It doesn’t replace Python for file-processing scripts of a few dozen lines, where writing time matters more than running time.
Popularity
The Stack Overflow Developer Survey asks programmers which languages they use and which they like. Rust has topped the “most loved” list for years running, but its user share is still well behind JavaScript and Python. I won’t give exact numbers because they change every year; look them up on the survey site.
The chart below is illustrative, not from the survey, just to show the order of magnitude between languages.
xychart-beta
title "User share (illustrative, not survey data)"
x-axis ["JavaScript", "Python", "C++", "Rust"]
y-axis "percent" 0 --> 70
bar [65, 50, 23, 13]
The 13 for Rust here is a number I made up for illustration, not a survey result. What’s true is the ranking: Rust sits behind those three languages in user count.
Start in ten minutes
Install via rustup, run cargo new hello, go into that folder and run cargo run. You’ll see Hello, world!. Then change src/main.rs to the array snippet above and run cargo build again to watch the compiler refuse it. That’s the clearest lesson in how Rust works: memory bugs are caught before the program exists, so users never run into them.