Rust design guidance - alternatives to linked data structures?

Greetings everyone

I am new to Rust (I have plenty of C experience and also Java/Kotlin/C#) and I am in need of some general data structure design guidance for Rust.

Reading some of the blogs and watching YouTube videos on Rust, when comparing it with C and C++ in particular.

In C and C++ we have all been taught to use multi-owned data structures such as linked-lists, double-linked-lists and (classic and modified) B-Trees.

Using the above cyclic/graph/linked data structures in Rust seems to be at least not the best practice and may even be considered too-hard, given the Rust ownership/borrowing/mutability rules.

So, what is recommended or best-practice to do in Rust for problems that we used to solve in C/C++ using the above pointer/linked data structures ?

many thanks!

2 Likes

If you need a general purpose collection, use a Vec, if you need an associative map, use a HashMap, if you can't then use a BTreeMap. If you need a collection if unique elements then use a HashSet first, if you can't then use a BTreeSet. If you need sub-ranges of of elements from the then prefer the BTree* variants. You almost never want a linked list.

1 Like