This is the first in (hopefully) a series of posts about my efforts to learn Rust! The goal is to track my progress and maybe see what techniques work better/worse.
Thoughts on Rust
Rust is a very cool language and definitely worth learning, if you already know C. If you don’t already know C, I think it would probably be a lot more frustrating. This is because a lot of Rust’s design, such as the ownership system, is based on high-level principles which are designed to eliminate problems you’d encounter in C. If you don’t know about those problems, the ownership system will feel dumb and obstructive instead of a cool formalization of good memory management practice. There are a lot of other things like this too - Rust concepts will feel natural, elegant, and expressive if you already understand the problems they were designed to address, but will feel unintuitive and pointless if you’re new to systems programming, because it’s much easier to map from systems constraints to Rust design principles than it is to map the other way.
Techniques
Technique 1: Reading the Book
The Rust project has a book to help you learn Rust. The book seems helpful as a reference, but I’ve found that reading through it linearly hasn’t helped much. My retention is poor, and I found as I moved into doing Rust projects that I would make mistakes that I had already read about in the book.
One related option I haven’t explored here is the interactive edition. Maybe that would be better for retention!
Technique 2: Reading Other People’s Code
A more useful thing I’ve been doing is reading other people’s code. This is nice because I get to see common Rust design patterns that would otherwise probably be hard to discover. Retention is a downside here too, but it’s not as bad because I get to see the concepts connect to each other in demonstrably useful ways.
A really nice aspect of this technique has been the ability to incorporate Bing chat (Microsoft’s GPT-4 search assistant). Though Bing chat has been pretty bad at writing anything more than basic Rust for me (and especially bad when crates with evolving APIs are involved), it’s quite good at reading Rust. So, for example, I learned about lifetime parameters by seeing them in someone’s code, asking Bing chat to translate that code, and then finding “lifetime parameters” in the book. Lifetimes are extremely common in Rust, and without this I would have had to wait to learn about them in Chapter 10.
Technique 3: Small Projects
The next thing I tried was implementing a small Rust project. I’m interested in mathematical optimization, so I wrote a little gradient descent project that computes the gradient of an objective function using the autograd
package and follows it to (in this case) maximize that function. This was super helpful, and was where I discovered a lot of the gaps in my knowledge. It’s also very motivating to write something that you’re personally interested in.
One weakness in this technique, though, is the scale of the projects. Rust is designed to help you write bug-free code for very big projects and work well with parallelism/multithreading. For example, the Result
struct in Rust is super useful for handling errors, but when your call stack is only 3-4 calls deep, it’s very tempting to just .expect()
or .unwrap()
all of your Results without actually handling errors. Doing this would leave a lot of value on the table.
Technique 4: Open Source Contribution
This is a thing I’m just starting with. I’ve chosen to contribute to Helix, since it’s a project I love and has a relatively mature community with plenty of open issues. I was also thinking about Typst, but sadly the software and community aren’t mature enough that I think it would work as well. I’ll update on this as I try it!
Technique 5: Re-Implementing Standard Library Components
This seems promising to me, but I haven’t given it a try yet either. I’m thinking of starting with Cow
, which seems relatively simple but also fundamental enough that I could learn a lot from it. Again, I’ll update on this after I give it a shot.
Edits:
05/18/2023: For those trying to learn Rust, I’ll try to update this page with excellent resources as I find them. Today I found Jon Gjengset’s Crust of Rust series, which has fantastic intermediate-level content about Rust-specific things (ex: his first video is on using multiple lifetime parameters in generic functions). You can watch Jon by write Rust code in real-time and answer viewer questions about it while he works, which offers insight into the design process you’d have a hard time getting otherwise.