Utility of `take_if`?

Rust 1.80 released take_if.

What are the biggest use cases? Or what's the backstory to why this was introduced? Which pain does it solve?

For the history, see Add `Option::retain` · Issue #70 · rust-lang/libs-team · GitHub and Add `Option::take_if` · Issue #98934 · rust-lang/rust · GitHub

1 Like

Here's the problem statement:

fn some_fn(values: &mut MyBigFlagStruct) {
    values.x -= 7;
    values.y += 42;

    if let Some(z) = &value.z {
        if *z < values.x * values.y {
            values.z = None;
        }
    }

    values.some_counter += 1;

    if let Some(magic) = &values.magic {
        if *magic != 42 {
           values.magic = None;
        }
    }
}

What are some real-world scenarios where this situation would occur? In my experience it seems like a rather uncommon situation, but perhaps it may be common in other industries or types of programs than the ones I've worked with.

Must be values.z with an s.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.