rdaum / rart-rs

An Adaptive Radix Tree implementation.
Apache License 2.0
36 stars 1 forks source link

Code suggestion: remove redundant `?` and `unwrap` #1

Closed jtroo closed 1 year ago

jtroo commented 1 year ago

Thanks for the work on this project!

I see a couple of instances where unwrap can be removed and replaced with only ?. As a general advice, unwrap can often be avoided and it is usually preferable to do so as a stylistic thing.

Two examples:


pub fn get<K: Key>(&self, key: &K) -> Option<&V> {
    self.root.as_ref()?;

    let root = self.root.as_ref().unwrap();
    AdaptiveRadixTree::get_iterate(root, key)
}

Could be written as

pub fn get<K: Key>(&self, key: &K) -> Option<&V> {
    AdaptiveRadixTree::get_iterate(self.root.as_ref()?, key)
}

        let next_node = cur_node.seek_child(k);
        next_node?;
        depth += cur_node.prefix.length();
        cur_node = next_node.unwrap();

Could be written as

        depth += cur_node.prefix.length()
        cur_node = cur_node.seek_child(k)?;

rdaum commented 1 year ago

Done, thanks