I’ve found myself recently hitting places where I had an Option<T> and wanted to pass some other component either a reference to the value in the Some case, or a newly-made default in the other case. This is a great time to use Cow, but the resulting code seems more verbose than the use-case demands:
let foo: Option<String> = Some("hello".into());
foo
.as_ref()
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned("hi".into()));
// or
foo.as_ref().map_or_else(|| Cow::Owned("hi".into()), Cow::Borrowed);
Proposed New API
Add borrow_or_else<'a, F>(&'a self, f: F) -> Cow<'a, T>, which returns Cow::Borrowed if self was Some, and Cow::Owned if it was None. Similarly, add borrow_or<'a>(&self, fallback: T) -> Cow<'a, T> and borrow_or_default<'a>(&'a self) -> Cow<'a, T> where T : Default.
Frankly, when using Cow<str> in this way, I tend to write unwrap_or(Cow::Borrowed("..."); in most cases I want to shove a string constant and defer allocation until I actually start writing.
I thought about that, but I previously struggled with using Cow, and I don't think I would've found this had it been a free function on Cow. Also, I think semantically it's clearer to start from an Option and either borrow it or produce an Owned fallback value.
In my case, there was parsing involved in the fallback branch so the or_else semantic was pretty important, but I think also adding a borrow_or makes sense.