Rust: A humbling experience
I just finished reading “The Rust Programming Langauge” and it was a humbling experience. I had to learn rust for work, being a new hire they wanted me on a language that enforced the writing of performative code that was (mostly) free of bugs.
Having toyed with C I understood the intrigue; memory safety without ‘free’ or without a garbage collector, performance without segfaults, and concurrency without race conditions.
The reality of RUST (in my opinion) is more nuanced than that.
The Ownership Model (aka welcome to hell)
In my view this was a game changer and forced me to rethink the way I wrote code. No longer can I abuse mutability, I had to actually consider the overall dataflow.
What is it? This is rusts answer to memory control. Declare a var, its immutable by default, and when it’s scope ends it’s freed from memory. Seems simple, but becomes complex very quickly.
At first I hated it. I felt like I was fighting with the compiler. It just wouldn’t let me do what I wanted. But then as I spent more time in the language I realised the compiler is my buddy. It’s stopping me from shooting myself in the foot.
A language like JS will give me some rope to hang myself with and then help me onto the stool. Rust however slaps the rope out of my hands and tells me to “be better”.
Error Handling is Dope
Another opinion: rusts Result<T,E> is probably the best approach to error handling I’ve personally encountered.
Every function that can fail returns a result and you can explicitly decide to handle the error, propagate it, or panic it. And the ? operator makes error propogation clean without hiding anything.
fn might_fail() -> Result<String, Error> {
let data = read_file("config.toml")?; // Propagate error if file read fails
let parsed = parse_config(&data)?; // Propagate error if parsing fails
Ok(format_config(parsed)) // Return success case
}
chefs fucking kiss
Fearless Concurrency
“Fearless concurrency” is real, but it doesn’t mean you can’t write bad parallel code. What it means is: you can’t write unsound parallel code.
The borrow checker enforces thread safety at the type level. No aliasing mutable references across threads, no dangling pointers sneaking through.
But Rust doesn’t save you from poor design. You can still deadlock your mutexes, starve your tasks, or thrash your scheduler. Rust guarantees memory safety and data race freedom, not correctness
This was my main lesson. You still need to write good code, Rust just won’t let you write broken code.
Pattern Matching
I feel like match expressions are what switch statements wish they could be. Exhaustive pattern matching means the compiler ensures you handle every case.
match result {
Ok(value) => process(value),
Err(NetworkError::Timeout) => retry(),
Err(NetworkError::NotFound) => handle_missing(),
Err(err) => log_and_continue(err),
}
butter
Traits
Traits are Rust’s version of interfaces, except they can do so much more. Defaults, generics, blanket implementation. Sharp abstractions!
I am not utilising traits enough in my code, because I haven’t spent the time with them. It’s something I plan on improving in future.
A Summation of Rust
I love it. Might not be for you.
Where RUST crushes it:
- CLI tools
- Anything perfomative, especially web with async
- Systems where reliability is critical
- Data processing pipelines (iteration chains)
- Getting newbs on the team to write good code (me).
Where it’s less useful:
- Rapid prototyping (strict compiler means more code verbosity)
- Heavy string manipulation —> rust don’t fuck around: UTF-8 or Grapheme? 1 or 2 bytes? You better be fucking sure becuase rust won’t compile shit unless you are.
- Any domain where speed is more important than stability or performance.
Should you learn it?
- Yes if you are: building performant systems, building infrastructure, working on DevOps tooling, a masochist.
- No if you are: web devving, building internal business tools, requiring your sanity intact.
Jokes aside: Rust is a solid langauge and has it’s place as a niche systems language anywhere performance and reliability both matter. When I hear that Dropbox, Discord and Cloudflare having starting using it for performance-critical infrastructure I am not surprised.