I have always worked with languages with automatic memory management. Did a little bit C++ when I studied, but didn't dive deep into C++ and manual memory management. But I always had an interest in system programming and how memory management works.
When I now look at code to do manual management in various programming languages I don't feel like I always understand what is going on. Only reference counting in Objective-C cause me trouble with protection violations due to too early deallocation.
So my idea is to "learn" manual memory management first using Rust. Once I got an understanding this way I can apply it to other languages as well (if I wanted to). My question is whether this is a feasible approach or unnecessary efforful. Problem from what I understand is that in Rust things are more restrictive and less flexible in some ways than in languages where the compiler cannot verify correct memory management.
From my own experience, I can say that this works wonderfully.
I had a small C course at beginning of my bachelor. We learnt some manual memory management there, but not that much. C is (at least on the surface) a simple language. And crucially, it allows you to shoot yourself in the foot, at worst without you even noticing.
The thing is, Rust is more restrictive about memory than C and C++. But for a good reason! Most of the code the Rust compiler forbids you from writing is actually illegal in those language as well. I have learned so much more about memory management from learning Rust than I did from that C course[1]. In the beginning, while still fighting with the borrow checker, I sometimes tried to do stuff and the compiler wouldn't let. At that point, I'd try to understand why the code is not correct and learn something new about (manual) memory management. In that way, I personally think Rust is a better language to learn about memory management than C. If you want, you can drop down to unsafe and do (mostly) everything you could do in C. The link Alice posted is a great resource for that. But just using normal safe Rust, encountering errors and learning why that was not allowed is also very good at teaching you this stuff.
This is also in parts due to the great compiler errors and learning material available for Rust.
To be fair, I'm programming in Rust for a couple of years now, and that course was only a couple of weeks. ↩︎
Alice, this is a real good one :-). Thanks. At the moment I read your reply I had to grin, because the link you provided made me remember that I once developed a doubly linked list in a programming course when I was studying back then.
Developed it in Modula-II on a VAX terminal. Now you know how long time ago that was ;-). Yeah, took a while till it didn't crash any more and till it looked clean (somehow "symmetric", you know).
Note that Rust doesn't really have manual memory management in practice. Unless you are allocatingg manually and writing unsafe and manual Drop impls, memory (and all other resources in general) are managed for you by the compiler. That's actually the same as RAII in C++, except that in C++, manual memory management along with RAII is still prevalent in legacy (or even new!) code bases or due to bad habits.
In contrast, you don't normally manually manage memory in Rust. It's mostly unnecessary because the standard library contains collection types and data structures required for everyday tasks.
I think discussing whether Rust's memory management is automatic or manual is much less useful then just stating the fact: if you can structure your program in a way that borrow checker would like it then translating it to manual memory management language is not hard… but the opposite is not 100% true.
I also read somewhere (it may have been here) that a dev who had spent some time in Rust, having come from C++, then went back to C++ and wrote much better C++ as a consequence.
This applies for many programming languages. Many users in these forums have reported that they start writing better code in their original programming languages after they learn to code in Rust.