Thread with 6 posts
jump to expanded postWhen I need to find a minimum time, minimum screen co-ordinate, etc, I often find myself writing Rust code like this:
if let Some(new) = new {
current = Some(current.map(|i| i.min(new)).unwrap_or(new));
}
Is there a better way to do this? Even if it just covers the inner part (the case where new.is_some()), I’d love to know.
a friend told me about map_or, and made some other suggestions: https://twitter.com/mcpowr/status/1625080741657985025
ah, it’s possible to do the whole thing like this:
current.xor(new).or_else(|| current.zip(new).map(|(a, b)| a.min(b)))
but that’s far from elegant… having .zip_with() would help a bit.
this kind of reduction case can’t be that uncommon. I wonder if it would be a good candidate for a new function? no idea what to call it, definitely not .xor_or_zip_with() lol
@hikari sentient_rust_utils::do_the_specific_thing_i_need_done()
@elena oh I love that function, it even provides a formal proof that your code is correct and automatically uploads a paper it co-authored to arXiv