I loved learning the Elixir language and how its pragmatic supervision trees and process model taught me the value fault tolerance as a quality of code than of infrastructure. Having safety and failure recovery as an idiomatic culture and mindset of the language made me a better thinker and developer. As a personal preference then in selecting new languages to learn, I look for potentially new perspectives and insights that it ascribes to its pilgrims. In general, a good learning curve is a good indicator since it has much to teach.

Another language to learn that I believe is worth learning is Rust, a system programming language with a strong culture of memory safety. Its pervasive model of ownership and lifetimes alongside its demanding yet helpful compiler makes compiling code both a challenge and thrill. Aside from the higher learning curve, the absence of a garbage collector and virtual machine while proclaiming safety and performance in the face of concurrency is boldly insane. If I do invest, I expect to be more acute about memory issues and be more confident with algorithms.

After reading the main online book, the best way to understand a language for me is by writing code with a project. Since Rust is a safe alternative to the C language, I want to revisit the data structures and algorithms that I avoided because of tricky pointer management. In starting with linked lists, I stumbled across Too Many Linked List online book which was godsend. Reading it next, I want to step it up and implement a tricky data structure that had interesting mechanics to prove my understanding: AVL Trees.

To understand my intent to go back to the basics, I scoffed at C for being too unsafe relative to Java's automatic memory management during my teenage years. What foolish thoughts. Nonetheless, it pushed me away from learning lower level algorithms which I thought of a hole in my knowledge for years. With this regret, I ran cargo new --lib avl_tree_set_rs .

After three weeks of struggling and tangents, I successfully wrote it with green tests. I found my trial a rigorous yet enlightening learning experience. Despite all the resistance and confusion from the compiler, I trust that as long as it compiles, my code and more importantly I feel safe, a similar feeling in Haskell. In this article, indulge me in my experience of implementing a self-balancing binary tree in Rust as a learning project. Hopefully, this is worth your time, helpful or entertaining. So what does it take to implement an AVL Tree in Rust?

(Source code for this article can be found here.)

REQUIREMENT:: This article assumes a rustc version of at least 1.38 for the non-lexical lifetimes if you want to compile or follow the code.

CAVEAT: This is not a tutorial on Rust. I assume you have read the book yourself and somewhat familiar with binary trees or sets. Although my writing may not be technically precise, I want to focus on discussing Rust from a newcomer's perspective. I do my best to link relevant sections from the book to help me explain syntax and concepts, so do visit them if you need to refresh yourself. You can also try running rustup doc --std that opens a local copy of the standard documentation. I found it quite helpful and convenient in understanding functions and methods. If I make any incriminating errors in terminology or code, do file an issue in the article's repository, so we can rectify it and my thanks.