Learning a system programming language: Rust

Learning a system programming language: Rust

I have spent the past few weeks learning Rust through the official book: The rust programming language, and enjoyed the experience. It is written by Steve Klabnik, one of the former contributors of Rust.

I have started learning Rust in 2019, abandoned it, and only returned to it recently.

Why systems programming?

I have been doing high-level web development for 3 years, and I wanted to work with technology at a lower and deeper level, which systems programming

Why Rust?

Besides being the most-loved language for consecutive years, Rust is designed to match the performance of C++, while providing memory safety, which is a safeguard for programmers against making such mistakes.

Interestingly, it is able to do so without the use of a garbage collector, through a concept known as ownership, and is one of the unique selling points of Rust.

The Rust programming language book

As mentioned, the book is well written and covers everything you need to get started with Rust - data types, control structures, error handling, tests, packaging, concurrency, mini-projects.

Here are some of my favourite chapters:

4. Understanding ownership

The concept of ownership is new and is Rust's unique selling point. The idea is that any heap-allocated data can only have 1 owner, so that freeing up the memory is straightforward.

This new way of approaching memory deallocation is new and takes time to get used to.

6. Enums and Pattern Matching

Pattern matching using the match keyword enforce exhaustibility, i.e. Rust makes sure we handle every possible pattern, otherwise the program won't compile.

Together with pre-defined enums such as Option, working with complex conditionals and variables that may or may not contain a value becomes a simple task.

9. Error Handling

Building on enums and pattern matching, handling recoverable errors is a joy with the Result enum, which contains the Ok variant if an operation succeeds, and the Err variant if it fails.

For uncoverable errors, simply use the panic! macro.

15. Smart pointers

Smart pointers are pointers that behave like references, but with additional capabilities such as dereferencing coercion, cleaning up resources after they are no longer needed.

Using a smart pointer such as Rc allows a value to have multiple owners, and they are tracked and deallocated accordingly.

16. Fearless concurrency

Concurrent programming is important to any large-scale, high-load systems.

The concept of channels(similar to golang) allows threads to pass messages between one another, and mutex to allow threads access to shared resources.

12. An I/O project: Building a command line program and 20. Final project: building a multithreaded web server

These 2 chapters walk through the process of building simple projects using the concepts learned so far and apply them in the context of proper software engineering practices such as SOLID, DRY.

Summary

I enjoyed the process of learning Rust and how memory safety is guaranteed through the ownership rule.

I may not use it for work any time soon, however this new way of thinking about memory has helped me become more aware of what goes underneath the hood when certain types of variables are created.

Also, I intend to continue exploring down the technology layers with systems programming and eventually use it as part of my work.

Did you find this article valuable?

Support Yap Han Chiang by becoming a sponsor. Any amount is appreciated!