Vec retain expose a &T but could expose a &mut T

Hey,

I just talked about the fact that I found Vec::retain a little bit too restrictive in the sense that it could have exposed a &mut T to the user instead of an immutable one.

Do you think that it could be ok to introduce a Vec::retain_mut method?

// implemented on Vec<T>
fn retain_mut<F>(&mut self, mut f: F) where F: FnMut(&mut T) -> bool;

Thank you in advance.

2 Likes

No, it would be a breaking change. For example the following code would not work anymore:

fn test(vec: &mut Vec<i32>) {
    vec.retain(|&n| n == 0)
}

OP is talking about introducing a new method (retain_mut), not changing retain. That wouldn't be a breaking change.

2 Likes

I swear I read another thing... Looks like this evening I'm too tired to even read properly. Sorry for the oversight

1 Like

Previous discussion.

2 Likes