Lesson 1 of 9 · 24m
Ownership Is Resource Management, Not a Compiler Puzzle
Read ownership as a contract about who releases resources, who may observe mutation, and where invariants are enforced.
The day-one advice I give new Rust hires is simple: stop asking "how do I make the borrow checker happy?" Ask "who owns this resource, and who is allowed to observe it while it changes?" The compiler is not the enemy. It is the first code reviewer that never gets tired, never forgets a lock, and never assumes a comment is true.
Ownership is broader than memory
Rust ownership applies to every resource with a lifecycle: heap memory, file descriptors, sockets, database transactions, temporary directories, mmap regions, locks, and cancellation tokens. This type owns the resource:
use std::fs::File;
fn write_report(path: &str) -> std::io::Result<()> {
let mut file = File::create(path)?;
use std::io::Write;
file.write_all(b"status=ok")?;
Ok(())
} // file closes here, even if write_all returned early with `?`The important part is not that file disappears. The important part is that cleanup is tied to scope, deterministic, and difficult to forget. In C or Go you enforce that with convention. In Rust the type system carries the obligation.
Moves transfer responsibility
A move is not a copy. It is a transfer of responsibility:
let socket = std::net::TcpStream::connect("127.0.0.1:9090")?;
let writer = socket; // responsibility moves to writer
// socket is no longer usable hereWhen an API takes a value by move, it is saying "I now control the lifecycle." When it takes &T, it is borrowing a read-only view. When it takes &mut T, it is borrowing exclusive permission to mutate. That is API documentation the compiler can enforce.
Shared mutation is the real hazard
Most ownership confusion is not about pointers. It is about aliasing plus mutation. Two parts of a program may each be correct in isolation, yet together invalidate assumptions. Rust's rule — many readers or one writer — is a systems-design rule before it is a language rule.
This is why cloning is sometimes the correct engineering choice. If ownership complexity costs more than the memory cost, clone small configuration values and move on. Do not clone sockets, buffers, or large object graphs just to silence design feedback.
Model destructive transitions by value
Consumption is expressive. A builder that becomes a server, a transaction that commits, or a probe that finishes should often consume self:
impl Transaction {
pub fn commit(self) -> Result<(), Error> {
// `self` cannot be reused after this call
self.inner.commit()
}
}That signature prevents an entire class of use-after-finalize bugs without runtime state checks.
Field notes
Dropis deterministic but not a substitute for protocol correctness; a dropped connection is not a graceful shutdown.- Do not use
Rc<RefCell<T>>as a reflex. It usually means ownership boundaries were never decided. - A type that owns a resource should usually be small and focused; large omnibus structs hide lifecycle responsibility.
- When you feel tempted to clone deeply, redraw the ownership graph before changing the code.
Was this lesson helpful?