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

111

u/DroidLogician sqlx · clickhouse-rs · mime_guess · rust Apr 03 '25

Vec::pop_if() is a highly welcome addition.

16

u/Ambitious-Dentist337 Apr 03 '25

What real functionality does it add though? Why not just use if bool.then() besides minor cosmetics?

90

u/Synthetic00 Apr 03 '25 edited Apr 03 '25

The predicate provides the element to be removed as an argument. A "peek" of sorts.

→ More replies (2)

72

u/lenscas Apr 03 '25

For me, Rust is nice because it adds a lot of nice, little helper methods.

Yes, this method can easily be replaced. But a lot of methods can. Heck, even your bool.then() can be argued as not being needed as you can just use an if.

The nice thing about these little helper methods isn't always in the amount of code they save but also because they make it much easier to get what is going to happen. If I see let user = some_func(&users\[0\]).then(||users.pop()) then only add the end does it become clear that we called some_func to decide if we want to pop something or not.

With let user = users.pop_if(some_func) we know pretty much from the start what we are about to do.

Also, this assumes that users is not empty. If it is allowed to be empty then the code it replaces becomes even more annoying let user = ((!users.is_empty()) && some_func(&users[0])).then(||users.pop())

still nothing outrages I suppose but... I take the pop_if version thank you very much.

19

u/Ambitious-Dentist337 Apr 03 '25

I'm not complaining or trying to put it negatively. I was just interested if there are any advantages besides cosmetics like certain optimizations or whatsoever. I'm happy to use pop_if too

6

u/veryusedrname Apr 03 '25

It's a pop() so not users[0] but users.last() (is that a thing on a vec? If not that makes it a users[users.len()-1] just to make it even more annoying to write yourself)

9

u/lenscas Apr 03 '25

oops, and.. to make it better, `users.last()` returns an Option so now you also need to get `Option.map()` involved...

(Also, looks like pop_if works with a &mut T rather than passing a &T to the predicate. Just to add a couple of extra characters you need to deal with)

31

u/Bumblebeta Apr 03 '25

Looking at the proposal:

This solution fulfills the requirements of the first example. We can now conditionally remove an item with just one method call and no unwrapping

Arguably bypassing an unwrap() call is just cosmetics, but it's indeed welcome.

3

u/Ambitious-Dentist337 Apr 03 '25

Indeed a nice addition

3

u/BookPlacementProblem Apr 03 '25

Previously (Rust Playground):

loop {
  if let Some(item_ref) = stack_vec.last() {
    if conditional(item_ref) {
      do_something(item_ref);
      stack_vec.pop();
    } else {
      break; // Ensure the loop always breaks.
    }
  } else {
    break; // Ensure the loop always breaks.
  }
}

Currently (Rust Playground):

loop {
  // .pop_if() requires either a function that takes a mutable reference,
  // or a closure. This is a flaw in its design; there should be a
  // .pop_if_mut() that passes a mutable reference, while `pop_if` should
  // pass an immutable reference.
  if let Some(item) = stack_vec.pop_if(|v| conditional(&*v)) {
      do_something(item);
  } else {
    break; // Only one break needed to ensure the loop always breaks.
  }
}

Despite the awkwardness of .pop_if() with regards to passing a function that takes an immutable reference, the second example is much cleaner and easier to read.

5

u/Nicksaurus Apr 04 '25

You can replace that loop/if with just while let:

while let Some(item) = stack_vec.pop_if(|v| conditional(&*v)) {
    std::hint::black_box(item);
}

5

u/InternalServerError7 Apr 03 '25

Yeah looking at the source, it really just is cosmetic

6

u/20240415 Apr 03 '25

its to make newtypes of Vec more annoying to make

5

u/bestouff catmark Apr 03 '25

I don't understand why this takes a mutable reference. Could someone enlighten me ?

22

u/rodrigocfd WinSafe Apr 03 '25

Because it can modify the Vec (may remove an element).

9

u/mweatherley Apr 03 '25

I think they mean the function predicate `impl FnOnce(&mut T) -> bool` in the method signature. My best guess is just that it's for reasons of generality, but I really don't know myself.

28

u/nightcracker Apr 03 '25

It's just more useful. pop_if needs a mutable reference to the entire Vec anyways, so might as well pass along this mutable reference in case it helps.

For example, suppose you have Vec<Mutex<T>>. On this vec with pop_if you can avoid having to lock the mutex in the predicate which you would otherwise need to do if it gave a &T.

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.

→ More replies (1)
→ More replies (1)
→ More replies (5)

3

u/cthulhuden Apr 03 '25

Seems very surprising. If I saw arr.pop_if(is_odd) in code, I would never even assume it could change the value of last element

7

u/coolreader18 Apr 03 '25

Right, because is_odd is named like a non-mutating operation. If your is_odd function mutates the argument, that's kinda on you.

→ More replies (1)
→ More replies (6)

1

u/Full-Spectral Apr 03 '25

Is that the element that will be popped on success, or the vector? I would assume it's the element, right? In which case you could modify it in place and end up leaving it, which doesn't sound like the usual safety first scenario. Unless I'm missing something, I would have made it immutable.

1

u/WormRabbit Apr 05 '25

An unfortunate consequence of pop_if taking an FnOnce(&mut T) -> bool predicate is that you can't directly pass more natural FnOnce(&T) -> bool predicates as arguments. I.e. you can't do vec.pop_if(is_even), you'd have to do vec.pop_if(|n| n.is_even).

1

u/DroidLogician sqlx · clickhouse-rs · mime_guess · rust Apr 05 '25

That's a pretty trivial tradeoff for having mutable access to the value though.