Ownership and Borrowing in Rust
by Hexagon, 3 min read rust guide
Ownership is the one big idea that makes Rust different from other languages. It's how Rust manages memory without a garbage collector, and how it prevents a whole family of bugs before your program even runs. It takes a while to get used to, so take your time with this one.
Why Ownership?
Every program needs to manage memory. In JavaScript, a garbage collector runs in the background, finds objects that nobody uses anymore, and frees them. That's convenient, but it costs time and memory. In C, you free memory by hand, which is fast, but easy to get wrong. Freeing too early, or twice, leads to crashes and security holes.
Rust takes a third path. The compiler keeps track of who is responsible for each value, and frees it at exactly the right moment. There's no garbage collector, and no manual freeing. The price is that you have to follow a few rules.
The Three Rules
- Each value has a variable that owns it, called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value is dropped, and its memory is freed.
String::from creates a String, which is text stored on the heap, that can
grow and change. We'll look closer at strings in the next article.
Moving
Here's where Rust starts to feel different. When you assign a String to
another variable, ownership moves:
In JavaScript, a and b would point to the same string. In Rust, there can
only be one owner, so after the move, a can't be used anymore. That's what
makes rule 3 work: when b goes out of scope, the string is freed once, and
there's no a left over pointing to freed memory.
The same thing happens when you pass a value to a function:
// text is dropped here
Cloning
If you really want two independent copies, ask for one with .clone():
Cloning copies the data, which takes time and memory. That's why Rust never
does it behind your back. When you see .clone(), you know a copy is made.
Copy Types
You might wonder why this works:
Simple values that live entirely on the stack, like integers, floats, bool,
char, and tuples and arrays of those, are Copy. Copying them is so cheap
that Rust just does it, instead of moving.
Borrowing
Moving values into every function would be tiresome, as you'd never get them
back. Instead, you can borrow a value with a reference, written &:
The function gets to look at the string, but doesn't own it. When the function
returns, nothing is freed, as the owner is still main.
Mutable references
A plain reference is read-only. To let someone change a value, lend it with
&mut:
Note that mut is needed in three places: on the variable, when lending, and
in the function's parameter type. You always see where things can change.
The Borrowing Rules
At any given time, you can have either:
- any number of read-only references (
&T), or - exactly one mutable reference (
&mut T).
But never both at once. And a reference can never outlive the value it points to.
Why is this a problem? push might need more room, and move the whole list to a
new place in memory. Then first would point to memory that has been freed. In
C, this is a classic bug. In Rust, it simply doesn't compile.
The borrow only lasts until the last time the reference is used. So this works fine:
The same rules prevent data races between threads, which we'll come back to in the last article of the series.
Slices
A slice is a reference to a part of a collection. For text, the slice type is
&str, which you've already seen:
Taking &str instead of &String is a good habit. A &String converts to a
&str automatically, so the function now accepts both a String and plain
text like "hello". Don't worry about Some and None yet, they're covered in
the article about enums.
Fighting the Borrow Checker
The part of the compiler that checks these rules is called the borrow checker. In the beginning, you'll argue with it a lot. Some tips:
- Read the error message. Rust's errors are long because they're helpful. They usually point to both the borrow and the conflicting use.
- Borrow instead of moving when a function only needs to read a value.
- Clone when it's cheap and you're stuck. It's not cheating. You can optimize later, if it ever matters.
- Keep borrows short. Use the reference, and let it go, before you change the value.
In Summary
*text means "the value that text points to". It's needed here, as we replace
the whole string, and not just call a method on it. format! works like
println!, but gives back a String instead of printing it.
If this article felt like a lot, that's normal. Ownership is the steepest part of learning Rust, and it gets easier with every program you write.