Picking Up Rust When You Already Know C/C++ and Go
So you already know some C or C++, you've written a decent amount of Go, and now everyone keeps telling you to learn Rust. Is it actually worth it? And if it is, what's going to trip you up? I used Rust for the dithering effect on my home page, and coming at it from C and Go, a lot of it felt familiar right up until the compiler started telling me no. So in this post, I wanna go over what's fundamentally different about Rust, where it really is the better tool, where it isn't (because there are plenty of those cases), and the mistakes people coming from C/C++ and Go tend to make.
Three ways to manage memory
The easiest way to understand Rust is to think about how the languages you already know handle memory.
In C, you manage it yourself. You call malloc, you call free, and if you get it wrong, that's on you. C++ gives you better tools for this, like destructors and smart pointers, but nothing actually stops you from holding a raw pointer to something that's already been freed.
In Go, the garbage collector manages it for you. You never free anything. The tradeoff is that there's a runtime running alongside your program, and every once in a while it has to stop and clean up.
Rust takes a third path. There's no garbage collector, and you (mostly) don't call free either. Instead, the compiler tracks who owns each piece of memory, and frees it when the owner goes out of scope. And the important part is that it checks all of this at compile time. If your code could use memory after it's been freed, it doesn't compile. So you get roughly C-level performance and control, without most of the C-level foot guns. That's basically the whole pitch.
What's fundamentally different
Ownership: every value has exactly one owner
Let's start with something that looks totally harmless:
fn main() {
let a = String::from("hello");
let b = a; // ownership of the heap buffer moves to b
println!("{a}");
println!("{b}");
}
In Go, this would just copy the string header and both variables would work. In Rust, it doesn't compile:
error[E0382]: borrow of moved value: `a`
3 | let b = a;
| - value moved here
4 | println!("{a}");
| ^^^ value borrowed here after move
A String owns a buffer on the heap, and only one variable is allowed to own it at a time. Assigning a to b moves ownership, and after that, a is dead. When b goes out of scope, the buffer gets freed exactly once. No double free, no leak, no GC.
If you know C++, this is a lot like std::move, with one big difference. In C++, a moved-from object is still sitting there in a "valid but unspecified state" and you can still touch it. In Rust, touching it is a compile error. Simple types like integers and floats implement Copy, so they just get copied like you'd expect. And when you actually want a second copy of a String, you call .clone() and pay for it explicitly.
Borrowing: lots of readers, or one writer
You obviously don't wanna move ownership every time you pass something to a function, so Rust lets you borrow a value with a reference. A &T is a shared, read-only reference, and a &mut T is a mutable one. The rule is that at any given time you can have either as many &Ts as you want, OR exactly one &mut T. Never both.
That sounds kinda arbitrary until you see the kind of bug it prevents. This C++ compiles without a single warning, even with -Wall:
std::vector<int> nums = {1, 2, 3};
int &first = nums[0];
nums.push_back(4); // may reallocate and move every element
std::printf("%d\n", first); // first may now point at freed memory
When the vector runs out of capacity, push_back allocates a new buffer, copies everything over, and frees the old one. And first is still pointing at the old one. Maybe it prints 1, maybe it prints garbage, maybe it works fine on your machine for six months and then doesn't. Here's the same thing in Rust:
let mut nums = vec![1, 2, 3];
let first = &nums[0];
nums.push(4);
println!("{first}");
error[E0502]: cannot borrow `nums` as mutable because it is also borrowed as immutable
3 | let first = &nums[0];
| ---- immutable borrow occurs here
4 | nums.push(4);
| ^^^^^^^^^^^^ mutable borrow occurs here
5 | println!("{first}");
| ------- immutable borrow later used here
The part of the compiler that enforces this is the famous (or infamous) borrow checker. It's going to reject a lot of your code at first. But a surprising amount of the time, it's catching something real.
Lifetimes: references can't outlive what they point to
Here's a fun one, because all three languages handle it differently. Returning a pointer to a local variable:
// C: compiles, with a warning. Using the result is undefined behavior.
int *make_number(void) {
int n = 42;
return &n; // n lives on make_number's stack frame
}
// Go: totally fine. Escape analysis moves n to the heap.
func makeNumber() *int {
n := 42
return &n
}
// Rust: doesn't compile.
fn make_number() -> &i32 {
let n = 42;
&n
}
C lets you do it and hands you a pointer into a stack frame that no longer exists. Go quietly fixes it for you by putting n on the heap, which the GC will clean up later (you can actually see this with go build -gcflags=-m, which prints "moved to heap: n"). Rust refuses, because the reference would outlive the thing it refers to, and it suggests returning an owned value instead. Every reference in Rust has a lifetime, and most of the time the compiler figures them out without you writing anything. You only have to spell them out (that 'a syntax you've probably seen) when it can't tell on its own, usually in function signatures that take and return more than one reference, or in structs that hold references.
No null, no exceptions
Rust doesn't have null. A value that might be missing is an Option<T>, which is either Some(value) or None, and the compiler won't let you use the value without dealing with the None case. So no more nil pointer dereferences at 2am.
Errors work a lot like they do in Go, in the sense that they're just values you return. But instead of returning (value, err) and hoping everyone remembers to check err, you return a Result<T, E>, which is either Ok(value) or Err(error). And there's a ? operator that does the whole if err != nil { return err } dance for you:
use std::fs;
use std::io;
fn read_port(path: &str) -> Result<u16, io::Error> {
let text = fs::read_to_string(path)?; // returns the error early, like `if err != nil { return 0, err }`
text.trim()
.parse::<u16>()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
Personally, as someone who has typed if err != nil more times than I'd like to admit, this is one of my favorite things about the language. You still handle every error explicitly, it's just way less typing.
No garbage collector, but cleanup is automatic
If you've used RAII in C++, Rust's Drop trait will feel very familiar. When a value goes out of scope, its drop runs, so files close, locks unlock and memory gets freed at a predictable point in your code. Not "whenever the GC gets around to it" like in Go, and not "whenever you remember to call free" like in C.
Traits instead of interfaces (and templates)
Rust's traits are kind of a mix of Go interfaces and C++ templates. Like a Go interface, a trait defines a set of methods a type has to have. But unlike Go, where a type satisfies an interface just by having the right methods, in Rust you have to say impl Trait for Type explicitly.
And by default, generics in Rust work like C++ templates: the compiler generates a separate copy of the code for each concrete type (this is called monomorphization). That makes them fast, since there's no dynamic dispatch, but it's also one reason Rust compiles slowly and binaries can get big. If you want Go-style dynamic dispatch, you ask for it with dyn Trait.
Data races are compile errors
This is the one that really got my attention coming from Go. In Go, you can do this and it compiles just fine:
count := 0
for i := 0; i < 4; i++ {
go func() { count++ }() // data race
}
The only way you'll find out is if you run it with -race and the race actually happens during that run. In Rust, the equivalent code with threads just doesn't compile. The same ownership and borrowing rules apply across threads (plus two marker traits, Send and Sync, that say which types are safe to move or share between threads). So if you wanna share a counter, you have to say how:
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let count = Arc::new(Mutex::new(0));
let handles: Vec<_> = (0..4)
.map(|_| {
let count = Arc::clone(&count);
thread::spawn(move || *count.lock().unwrap() += 1)
})
.collect();
for h in handles {
h.join().unwrap();
}
println!("{}", *count.lock().unwrap()); // always 4
}
Arc is a reference-counted pointer that's safe to share between threads, and the Mutex actually wraps the data. That's a subtle but huge difference from Go, where a sync.Mutex sits next to the data and you just have to remember to lock it. In Rust, you literally can't get to the number without locking first.
Now, the flip side. Go's concurrency is built into the language: you type go and you're done. Rust's async/await has no built-in runtime, so for async code you pull in a crate like Tokio, and you have to deal with a split between async and regular functions. Rust is safer here, but Go is way simpler. More on that below.
Cargo
Quick one, but it matters. If you're coming from C/C++, Cargo is gonna feel like a gift. Building, testing, dependencies, docs, benchmarks and formatting are all one tool with one config file, no CMake required. If you're coming from Go, it'll feel about the same as go build and go test, which is a compliment.
Where Rust is actually the better choice
Okay, so that all sounds great. But Go is already memory safe, and C++ is already fast, so when does Rust actually win?
- Code that handles untrusted input. Parsers, file format decoders, network protocols, anything a stranger gets to feed data into. This is exactly where memory bugs turn into security holes. Microsoft and the Chromium team have both reported that around 70% of their serious security bugs are memory safety issues, and Google has said that as Android shifted new code to memory-safe languages (Rust being a big one), memory safety bugs dropped from 76% of Android's vulnerabilities in 2019 to 24% in 2024.
- Latency-sensitive services where GC pauses hurt. Go's garbage collector is really good, but it's still there. The classic example is Discord, who wrote about moving one of their services from Go to Rust in 2020 because of latency spikes every couple of minutes that lined up with garbage collection. If your p99 latency really matters, not having a GC at all is a real advantage.
- WebAssembly. Rust has no runtime to ship, so the modules come out tiny. The dithering module on my home page is about 24 KB. Just for fun, I built a Go "hello world" for WebAssembly while writing this, and it came out to 2.5 MB, because it has to bring the Go runtime and garbage collector along with it. (TinyGo does a lot better, but it's a different compiler with its own limitations.)
- Embedded and systems code. Rust can run without its standard library (
no_std) on microcontrollers, and the Linux kernel has accepted Rust code since version 6.1. Anywhere you'd normally reach for C because you can't afford a runtime, Rust is now an option. - Fast libraries for other languages. Rust can expose a C ABI, so it's become a popular way to write the fast parts of other ecosystems. A lot of modern Python tools and libraries are written in Rust, like Ruff, uv and Polars.
- CLI tools. Like Go, Rust gives you a single binary you can just copy somewhere and run. Tools like ripgrep are a good example of how fast that can get.
When Rust is NOT the better choice
I think this part gets skipped a lot, so let's be honest about it.
- Typical web backends. If you're building a CRUD API that mostly waits on a database, the GC is not your bottleneck, the database is. Go will get you there faster, compile faster, and be easier for the rest of your team to read. (This site is written in Go for exactly that reason.)
- Prototypes and exploratory code. When you're still figuring out what the code should even do, the borrow checker makes you commit to an ownership structure before you know what it should be. Refactoring Rust is very safe, but it's not fast.
- Teams that need to onboard people quickly. You can be productive in Go in a week or two. Rust takes a lot longer than that for most people, and that's a real cost, especially for a team.
- Big existing C++ codebases. Rewriting working code is risky, and C++/Rust interop is doable but takes effort. Writing new components in Rust is often a better move than a rewrite.
- Data structures full of pointers pointing at each other. Doubly linked lists, graphs with cycles, trees where children point back at their parents... the stuff that's easy in C and Go is genuinely awkward in Rust. You can do it, but you'll end up with
Rc<RefCell<T>>, indices into aVec, orunsafe. - When compile times matter a lot. Go compiles a big project in seconds. Rust can take minutes, especially release builds, and that adds up over a day of edit, compile, run.
Common pitfalls
Here are the things that tend to trip people up, especially people who already know C/C++ and Go and assume they can just carry their habits over.
1. Fighting the borrow checker instead of listening to it
When the borrow checker says no, the first instinct is to sprinkle .clone() everywhere until it goes away. Honestly, while you're learning, that's fine. A clone is just a copy, and copies are usually cheap. But if you find yourself cloning big structures or wrapping everything in Rc<RefCell<T>>, that's usually a sign that the code is structured like C or Go and needs to be restructured, with clearer ownership, instead of forced through the compiler.
2. Building object graphs with back pointers
This is the specific version of #1 that C++ and Go people hit the most. You've got a parent that points to its children and children that point back to their parent. In Rust, a really common answer is to store all the nodes in a Vec and have them refer to each other by index. It feels kinda weird at first, but it's simple, fast and cache friendly, and it's how a lot of real Rust code (including compilers and game engines) handles graphs.
3. unwrap() everywhere
.unwrap() takes the value out of an Option or a Result, and crashes the program (panics) if there isn't one. It's handy in examples and tests, but every unwrap() in production code is a crash waiting to happen. Use ? to pass errors up, or .expect("why this can't fail") when you really are sure. For applications, the anyhow crate makes error handling a lot less painful, and thiserror is great for defining error types in libraries.
4. Strings aren't byte arrays
If you're coming from C, where a string is basically just an array of chars, Rust strings are gonna surprise you. They're always valid UTF-8, and you can't index into them at all. s[0] is a compile error. And if you slice in the middle of a multi-byte character, it panics at runtime:
let s = String::from("héllo");
println!("{}", &s[0..2]); // panics: byte index 2 is not a char boundary; it is inside 'é'
Use .chars() to go character by character, or .as_bytes() if you really do want bytes. Also, there are two string types: String owns its data, and &str is a borrowed view into some string data. A good rule of thumb is that functions should take &str and structs should usually hold a String.
5. Putting references in structs too early
Related to the last one. As soon as a struct holds a reference, it needs lifetime annotations, and those annotations spread to everything that uses the struct. When you're starting out, just have your structs own their data. You can optimize later if you actually need to.
6. Integer overflow behaves differently in debug and release
Caution!!! This one is sneaky. In a debug build, integer overflow panics. In a release build, it silently wraps around, just like it would in C:
fn next(x: u8) -> u8 {
x + 1 // next(255): a debug build panics with "attempt to add with overflow", a release build returns 0
}
So a bug can crash your tests and then quietly do the wrong thing in production (or the other way around, where everything looks fine because you never hit that number in testing). If overflow is possible, say what you want explicitly with checked_add, wrapping_add or saturating_add.
7. Reaching for async before you need it
In Go, every network call is cheap to put in a goroutine, so it's natural to think concurrency first. In Rust, async adds a runtime, a whole ecosystem, and some of the most confusing compiler errors you'll ever see. Plain threads (or a library like Rayon for parallel loops) handle a lot more cases than you'd think. And if you do use async, don't call blocking code like std::thread::sleep or heavy CPU work inside an async task, because it blocks the thread that's supposed to be running everything else.
8. Thinking unsafe turns off the borrow checker
It doesn't. An unsafe block lets you do five extra things: dereference raw pointers, call unsafe functions, read or write mutable statics, implement unsafe traits, and access union fields. Everything else is still checked. So unsafe doesn't mean "this is C now", it means "I'm promising the compiler that these specific operations are fine." Keep those blocks small, and write a comment explaining why each one is actually safe.
9. Benchmarking debug builds
Plain cargo run and cargo build make unoptimized debug builds, and they can be dramatically slower than release builds. If you're about to write a blog post about how Rust is slower than Go, make sure you used --release first.
How I'd go about learning it
- Read The Rust Programming Language (everyone just calls it "the book"). It's free, and it's genuinely good. Chapters 4 (ownership), 10 (generics, traits and lifetimes) and 15 (smart pointers) are the ones that matter most for you.
- Do Rustlings, which is a bunch of small broken programs you fix one at a time. It's great for getting used to reading compiler errors, which honestly are some of the best compiler errors out there.
- Run Clippy.
cargo clippyis a linter that points out code that compiles but isn't idiomatic, and it'll teach you a lot about how Rust people actually write Rust. - Port something small you already wrote in Go or C. A CLI tool or a parser is perfect. You already know what the code is supposed to do, so all your attention goes to the Rust part.
So, is Rust worth learning if you already know C/C++ and Go? I'd say yes, even if you don't end up using it every day. Learning to think about ownership will change how you write C and Go too. Just don't expect it to replace Go for everything. It's a different tool for a different set of problems, and knowing when to reach for which one is honestly the real skill.