title: How to compose two calls to Regex::replace_all?
body: <p><a href="https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace_all" rel="nofollow noreferrer"><code>Regex::replace_all</code></a> has the signature <code>fn (text: &amp;str) -&gt; Cow&lt;str&gt;</code>. How would two calls to this be written, <code>f(g(x))</code>, giving the same signature?</p>
  <p>Here's some code I'm trying to write. This has the two calls separated out into two functions, but I couldn't get it working in one function either. Here's my <code>lib.rs</code> in a fresh Cargo project:</p>
  <pre class="lang-rust prettyprint-override"><code>#![allow(dead_code)]
  
  /// Plaintext and HTML manipulation.
  
  use lazy_static::lazy_static;
  use regex::Regex;
  use std::borrow::Cow;
  
  lazy_static! {
      static ref DOUBLE_QUOTED_TEXT: Regex = Regex::new(r#&quot;&quot;(?P&lt;content&gt;[^&quot;]+)&quot;&quot;#).unwrap();
      static ref SINGLE_QUOTE:       Regex = Regex::new(r&quot;'&quot;).unwrap();
  }
  
  
  fn add_typography(text: &amp;str) -&gt; Cow&lt;str&gt; {
      add_double_quotes(&amp;add_single_quotes(text)) // Error! &quot;returns a value referencing data owned by the current function&quot;
  }
  
  fn add_double_quotes(text: &amp;str) -&gt; Cow&lt;str&gt; {
      DOUBLE_QUOTED_TEXT.replace_all(text, &quot;“$content”&quot;)
  }
  
  fn add_single_quotes(text: &amp;str) -&gt; Cow&lt;str&gt; {
      SINGLE_QUOTE.replace_all(text, &quot;’&quot;)
  }
  
  
  #[cfg(test)]
  mod tests {
      use crate::{add_typography};
  
      #[test]
      fn converts_to_double_quotes() {
          assert_eq!(add_typography(r#&quot;&quot;Hello&quot;&quot;#), &quot;“Hello”&quot;);
      }
  
      #[test]
      fn converts_a_single_quote() {
          assert_eq!(add_typography(&quot;Today's Menu&quot;), &quot;Today’s Menu&quot;);
      }
  }
  </code></pre>
  <p>Here's the best I could come up with, but this will get ugly fast when composing three or four functions:</p>
  <pre class="lang-rust prettyprint-override"><code>fn add_typography(input: &amp;str) -&gt; Cow&lt;str&gt; {
      match add_single_quotes(input) {
          Cow::Owned(output) =&gt; add_double_quotes(&amp;output).into_owned().into(),
          _                  =&gt; add_double_quotes(input),
      }
  }
  </code></pre>