this post was submitted on 11 Aug 2023
1 points (100.0% liked)

The Rust Programming Language

1 readers
0 users here now

A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and...

founded 1 year ago
MODERATORS
 
This is an automated archive made by the Lemmit Bot.

The original was posted on /r/rust by /u/Velfke on 2023-08-11 14:58:26.


Hello, r/rust!

I've been following Rust on this subreddit for some time now. Finally, I decided to dive in, having had no prior experience with the language, and share my experiences here.

BLUF: Rust is amazing, and I'm excited to dive deeper.

1. Choice of Project

I chose to implement a topic close to my heart from university: functional data structures. Check out the library . It doesn't make use of unsafe or &mut self.

To briefly introduce, functional data structures are immutable by design. Once created, they remain unchanged. To modify them, you clone and then modify the cloned version. This ensures immutability and persistence (allowing retention of all versions of the data structure). The beauty lies in making the clone operation inexpensive by reusing most of the original data structure.

2. Rust's Strengths

2.1. Pattern Matching + Union Type

This combination greatly enhances the expressiveness and correctness of certain data structures. Consider:

pub enum AVL {
    Empty,
    Node {
        key: ...,
        value: ...,
        left: ...,
        right: ...,
    },
}

In many languages, the typical implementation might set "left" as a null pointer, introducing potential runtime bugs, or an Option::None, leading to cluttered conditionals. Rust allows the empty AVL tree to be a valid state, simplifying method implementations:

fn height(&self) -> i64 {
  match self {
      AVL::Empty => 0,
      AVL::Node { key: _, value: _, left, right } => 1 + max(&left.height(), &right.height()),
  }
}

Furthermore, this approach guarantees you consider all possible states for functions like find:

pub fn find(&self, target_value: &K) -> Option<&V> {
    match self {
        AVL::Empty => Option::None,
        AVL::Node { key, value, left, right } => 
            match target_value.cmp(key) {
                std::cmp::Ordering::Less => left.find(target_value),
                std::cmp::Ordering::Equal => Option::Some(value),
                std::cmp::Ordering::Greater => right.find(target_value),
            },
    }
}

2.2. Traits

The trait system truly endeared Rust to me. Having copy and clone disabled by default for user-defined types means you must opt into their implementation. This can guarantee, through the type system, that no unintended copies or clones are made. This pushed me to deeply reflect on my algorithmic assumptions: In a Trie storing [T], is T required to implement clone? Should T be Ord or is Eq sufficient?

3. Challenges Faced

3.1. Learning Curve

Grasping Rust felt like re-learning programming. Despite the exhaustive Rust Book and its top-notch compiler error messages and LSP, Rust is conceptually intense. It felt like simultaneously mastering a robust type system and the intricacies of systems programming, topics that could each demand an entire university course. I find it hard to envision Rust as a "black box" programming language.

My primary learning resource was the Crust of Rust YouTube series.

3.2. Nested Pattern Matching with Rc/Arc

Given the heavy reliance on sharing chunks of immutable data in these structures, there are many instances of Rc/Arc. This essentially obstructs nested pattern matching, especially when you're handling tree rotations and aiming to match specific scenarios. This challenge seems to be under active consideration.

4. Reflections

Going forward, I aim to expand this library and continue my Rust journey, with iterators on the horizon.

In hindsight, perhaps this project wasn't the most beginner-friendly choice; it thrust me into the depths of Rust's concepts like borrow checker, lifetimes, Rc/Arc, and generics. Regardless, I'm delighted with my experience. It confirmed that Rust isn't just hype—it genuinely enhanced my understanding of many systems programming nuances that I'd previously overlooked.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here