Gettin’ Rusty with Rust!


This week I am going to be talking about Rust! Given that this is my final quarter, I wanted to challenge myself a bit and try and learn a new language and build a fun portfolio project for the job hunt. I decided to go with Rust as I had heard good things about it from peers, and I enjoy working with low-level languages.

I decided to start off working through The Rust Programming Language book. It is the official documentation and walkthrough listed on the Rust website. They also provide alternative ways of learning the language depending on your learning pace and style. If preferred, you may work with small projects/examples rather than working through the book and documentation.

Overall, the draw about Rust that has really kept me staying the course has been that Rust addressed many of the frustrations that I had while learning and working with C. I always had a difficult time wrapping my head around malloc/calloc to properly allocate and deallocate memory and to ultimately avoid memory leaks in my programs. It was also frustrating learning how strings work in a low-level language.

Rust addresses these issues in a more approachable way. Though there is no garbage collection to address any leftover memory, Rust manages this issue with its’ concept of ‘ownership’. Every variable ‘owns’ the value it is initialized to. This variable owns this value as long as the variable remains within scope. As soon as it is out of scope, the value is dropped and the memory freed. This is so much easier to learn as you only have to keep track of what variables/values are in scope rather than having to keep tabs on whether the memory was properly allocated and freed. Additionally, if any of these rules are violated, Rust will refuse to compile and provide detailed errors for compilation issue.

Additionally, Rust makes string use very simple. Rust essentially breaks strings into two groups – statically allocated strings and heap allocated strings. If a string is not intended to change throughout the length of a program, say perhaps a block of text that will never change, then a statically allocated string will be the best fit (&str). If the length of that string is destined to change, say for user input, then you would use a heap allocated string (&String). Having only two cases to adhere to greatly simplifies how to use strings, as well as providing very memory efficient programs, given you can handle all statically allocated strings at compilation.

Overall, I am still on my Rust journey, but I sure am having fun on it! I hope to bring you more Rust knowledge in blog post #3. Until next time!

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *