My Experience With Learning Rust

Hello! This is my first blog post and hopefully, there will be more to come.
In this blog post, we will be talking about my experience in learning Rust in general.

The Rust Logo. Gotta admit, it’s pretty cool lookin’!

Background

I began to learn to program in my 6th-grade year of elementary school. I’m currently in my final year of high school, so that puts my programming career at around 6 years, but programming has always been nothing more than a hobby until recently. My first programming language was Visual Basic. Not the nice VB.NET, but the ugly VB4. I then moved between many BASIC based languages until I found VB.NET, finally. It was amazing. I shortly moved to C# with around 1 year of programming experience in. From here, I’ve continued using C# along with some other languages on the side. I then got into OS development and learned x86 assembly and C on bare bones. Then I lost that craze and moved to Rust about 5 months ago. After I started experimenting with Rust I was hooked. From then on, Rust has been my language of choice along with C# for my class in game development I take as an elective in school.

The Rusty Hook That Reeled Me In

Although I didn’t start writing this blog when I started writing in Rust, I remember clearly why I started writing in Rust. From what I had been doing in C#, none of it utilized multiple threads. Multi-threaded programming had always been a sore topic for me. Things like thread synchronization and message passing never really made sense to me. I learned Rust mainly due to its promises for ease of programming for multiple threads. Once hearing that things like parts of Firefox were written using it, and the Mozilla themselves were very strongly invested in Rust, I had more reason to start writing in it. I’d assume that if Mozilla were to invest so much in a programming language, it must have some sort of benefits over your usual C and C++ programming languages. Even further than that, I’ve heard of some game studios adopting Rust in their games and I was pretty much hooked at that point.

First Impressions

My first impressions of Rust and my first times using it to write things like “Hello World” and some more complicated versions of “Hello World” which I usually do which include things like Fizz-buzz and even a Chip-8 emulator, I was so pleasantly surprised with how easy it was to use. I forgot at a lot of times that this was a fully compiled language with no sort of garbage collection or anything. It was so easy to not really worry about when things were being destroyed. I was able to reap the benefits of ease of use of a garbage collected language like C# and the performance boosts of languages like C++ and C. Also, the really nice struct system of Rust and their system of inheritance called Traits was a very nice addition. It felt a lot like C# Interfaces. Their strong standard library made it easy to jump right in with things like vectors which felt much like C#’s lists. Of course, much like other beginners, I fought and fought with the borrow checker, but unlike most beginners which usually go towards things like Rc or Arc, I instead tried to change my program logic to follow the borrow checker much more. The compile-time errors helped a lot for showing me why the borrow checker was getting angry at me and was very invaluable in helping me understand exactly how the borrow checker worked. I felt that experimenting with what things the borrow checker would allow and what things it wouldn’t allow were the best way for me to learn how to use it.

Programming Style Changes

Because of Rust’s borrow checker, it forces a programmer to think differently. Although most of the time, you don’t have to worry about when values are being thrown away or when they drop out of scope, you do need to worry about when the value is being used and what’s using it. Learning how to deal with this has completely changed how I program in most cases. Thinking about things in a more data-oriented fashion instead of nice faces to hide ugly code, which C# really does best (whether that’s a compliment or an insult to the language is up to you to decide), is one of the most important changes that Rust has done to me. It’s helped me in my C# programming in not only cleaning up my code but also with performance issues I used to have with my C# programs, especially when working with Unity, where sharing data between different objects could cause performance hits like nobody’s business. Thinking about which data objects hold and which data other objects may use is usually an object-oriented way of thinking, but Rust makes you think about this in a more open manner. Instead of using things like accessors, setting the field to public was usually a better idea most of the time. Rust is very good at deciding on which fields of a struct are borrowed and which aren’t, and using something like an accessor would just cause the full struct to be borrowed, and when you’re trying to use different data from the same struct which is completely borrowed when you’re only getting one bit of data is just an all around bad experience in Rust. The fact that Rust makes things difficult to do is a good thing to me. It’s forced me to become a better programmer.

Overall Ease-of-Use as A Programming Language

The overall ease of use of Rust was a large selling point for me when starting and is a big selling point for when I’m trying to get other people to begin to learn Rust. Although the borrow checker is admittedly annoying and frustrating at first, it’s saved me from a lot of different situations such as use after frees and similar data associated programming errors. The struct system is extremely easy to use and intuitive. The standard library makes many things which are daunting in languages like C seem like a piece of cake. Things like list and hash maps are amazing in Rust and I’ve never had any hard times using them as I have with C. Their error system of Options and Results are invaluable to me. They’re a godsend compared to things like implementing exceptions in C#. The enum system of Rust is something I wish every language had. I find that whenever I’m working in C# in Unity, I wish I had the Rust enum system. The ability to associate data with their variant in an enum is something which took me a while to start to use, but once I did, I haven’t been able to live without it. Although I still cannot say how hard or easy Rust is for multi-threaded loads because, ironically enough, I still haven’t written any multi-threaded code for it, I have heard many a soul praising a crate called “rayon” for easy multithreading, and reading their documentation on how to use the crate, I would agree from the surface view. Along with the multithreading parts of Rust, there are many parts of the language which I haven’t used, not because I don’t find them to be good parts of the language, but because I haven’t had the need to use them yet. Things like closures and the unsafe side of Rust are just not necessary for the things that I write, but I know of many projects which gladly use them and usually praise their usefulness when they are needed (especially closures).

The Not-So-Rusty Rust Ecosystem

The Rust ecosystem is an ecosystem which rivals any sort of other programming languages I’ve used. Their packaging system, Cargo, has been the easiest packaging system I’ve ever used. It’s been easier than even npm and pip. Adding a dependency, or crate as it’s called in Rust terms, to your program is as easy as adding a line to a file. The crates which that crate depends on is managed automatically by Cargo and you’ll never have to worry about version conflicts. If one crate is needed by two crates that you have as dependencies in your program, but each crate requires a different version of that one crate, Cargo automatically downloads their respective versions and compiles them without you having to lift a finger. Although the collection of crates is small compared to the number of libraries available for languages like C or C++ (especially in the graphics side of things), the ecosystem is still young, and I personally see many new crates being released daily. The community is very learner friendly, and although its enthusiasm about the Rust language is a bit excessive sometimes, that enthusiasm gets channeled back into a love for the language and the enthusiasm shows in how helpful it can be to not only learners of the language, but people who have been using the language for years.

Conclusion

My experience with Rust has been, for the most part, extremely positive. Although there have been some walls in learning it like the borrow checker and their Trait system, I’ve learned a lot which has helped me grow not only as a Rust user but as a programmer in general. Rust has been a great language to work with and I hope to use it for years to come. I strongly recommend you go take a look at their website. You can also interact with the Rust community at the fourms or at the subreddit.