Thread with 6 posts

jump to expanded post

When 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.

Open thread at this post

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.

Open thread at this post