I'm trying to get into #rustlang. I've already rewritten a small program and it was an okay experience but today, I hit something weird.
I have a LinkedList and need to iterate over it and, if I found any item that meets some criteria, I need to remove it from the list.
Using C++ iterators, this would be O(N).
Rust's linked_list has an issue open for more than 4 years (!) to add a remove function to linked lists. But even with that function, you'll need an index of the item to be removed and cannot (as far as I can tell) use the iterator you already have from scanning the list.
So in Rust I would need to spend O(N) to scan the list and an additional O(N) for each item I need removed.
Or is there a faster way to do that?
#68705 adds a method in LinkedList to remove an element at a specified index. The feature gate for the issue is #![feature(linked_list_remove)].GitHub
That guide explicitly says I should use a LinkedList when:
You want to efficiently split and append lists.
Which is exactly why I want to use a linked list. Vec
seems like it would do a lot of unnecessary memory allocation for that.
Technically that's also O(n) (O(2n) = O(n)).
I don't see much Rust code using `LinkedList`. Probably its more common to just use `Vec`. Which in many cases will end up performing better overall, regardless of language.
I've been writing Rust for 5 years and professionally for 2 years, I've seen a linked list outside of some very specific use cases.
Yes, Rust Linked List are very limited and you will most likely fight the borrow checker.
Are you sure you need a linked list? In most cases a Vec will be more efficient, and you won't have as many borrow checking issue.
If the order of elements doesn't matter, you can even use Vec::swap_remove for O(1) removal of elements.
if you want to remove all element that match a specific criteria, try using Vec::retain.
https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.retain
https://doc.rust-lang.org/stable/std/vec/struct.Vec.html?search=retain#method.swap_remove
Vec in std::vec - Rust
doc.rust-lang.orgVec::retain
create a whole new copy of the collection, i.e. I'll need twice the memory at that point?I don't want to use Vec because:
Vec in std::vec - Rust
doc.rust-lang.org