Alvov1
Alvov1

Reputation: 307

How to remove an element from LinkedList in stable Rust?

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

Answers (1)

mousetail
mousetail

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

Related Questions