Closed TooManyBees closed 3 years ago
You may have to perform the cast when assigning best_so_far
: best_so_far = Some(thing as *const _)
.
It is safe since even mutable cursors only expose an immutable reference to elements in the collection.
That's great to hear, thanks.
I have a use case where I want to iterate through a linked list and remove the "best" item (by whatever metric). So it involves testing each item, maintaining a reference to the best one found so far, then afterward removing the best item from the list.
This is roughly the code I want to use. It works, but results in the warning https://github.com/rust-lang/rust/issues/59159, which will soon turn into a hard error, that
list
is being mutably borrowed whilereference
is already immutably borrowinglist
.I found that casting the reference to a
*const
drops the immutable borrow and makes the borrow checker happy, like so:Is this pattern safe at all? Not just casting the reference to a pointer, but the whole thing: maintaining a reference that was borrowed immutably to create a mutable cursor in order to remove the item. If it's unsafe, is there a better way to do what I'm trying to?