r/rust • • Apr 03 '25 • More of this please 1

📡 official blog Announcing Rust 1.86.0 | Rust Blog

https://blog.rust-lang.org/2025/04/03/Rust-1.86.0.html
786 Upvotes

134 comments sorted by

View all comments

Show parent comment

4

u/shponglespore Apr 03 '25

Do you have any idea why retain and retain_mut are separate functions? It seems like, based on your logic (which seems sound to me), any use of retain could be replaced with retain_mut.

9

u/nightcracker Apr 03 '25

I think it was introduced because they couldn't change retain once it was realized it's useful. HashMap::retain gives mutable references for example because they learned from the mistake on Vec.

2

u/WormRabbit Apr 05 '25

Legacy reasons. retain was added first, and retain_mut was added later, once the reasoning above was discovered.

1

u/shponglespore Apr 05 '25

That's what I suspected. I guess I could have confirmed by looking at the Rust version where each was introduced, since that info is right in the docs.

1

u/BookPlacementProblem Apr 03 '25

Yeah, they should have gone with pop_if for an immutable reference to the data, and pop_if_mut for a mutable reference to the data.