Proposal: Option::cow_or_else

Since this is an or situation, I think Something for coalescing; aka generalized/improved `or_else` would work:

    let foo: Option<String> = Some("hello".into());
    let bar = coalesce!(
        foo.as_ref().map(Into::into),
        Cow::Owned("hi".into()),
    );
    dbg!(bar);

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d253ee1d1945bc0ad25cdea574ed1ea3

I like that as it’s a more general thing, not a cow-specific additional method.

(Note that the type inference is flowing nicely through the macro – the first argument gets its Cow-ness from the second.)

1 Like