How to work with multiple `Cow`s?

This works Rust Playground

fn f2(s: Cow<'_, str>) -> Cow<'_, str>

The reason s.deref() won't work is like what's stated in this post:

  • s.deref() desugars to Deref::deref<'tmp>(&'tmp Cow<'f, str>) -> &'tmp str where 'f imposed on the f function strictly outlives 'tmp
  • then f2(s.deref()) is f2(s: &'tmp str) -> Cow<'tmp, str> and you'll get a shorter lifetime than 'f, thus the error
4 Likes