Reputation: 307
I have a linked list of values and I want to remove elements from there either by value or by index. I tried to use the remove function, but the compiler says that this function is not stable. Is it possible to remove elements from an arbitrary location in a linked list using only stable Rust?
Upvotes: 1
Views: 744
Reputation: 8010
You can split the array using split_off
then join them back together without the element. See this comment on github
// Split the list in 2 let split_list = original_list.split_off(index_to_remove); // Remove the first element of the second half split_list.pop_front(); // Join the 2 halves back together, except for the middle element original_list.append(split_list);
Upvotes: 4