title: With closures as parameter and return values, is Fn or FnMut more idiomatic?
body: <p>Continuing from <a href="https://stackoverflow.com/questions/60173959/">How do I write combinators for my own parsers in Rust?</a>, I stumbled into this question concerning bounds of functions that consume and/or yield functions/closures.</p>
  
  <p>From these <a href="http://babelmonkeys.de/~florob/talks/RC-2018-09-05-fn-traits.pdf" rel="nofollow noreferrer">slides</a>, I learned that to be convenient for consumers, you should try to take functions as <code>FnOnce</code> and return as <code>Fn</code> where possible. This gives the caller most freedom what to pass and what to do with the returned function.</p>
  
  <p>In my example, <code>FnOnce</code> is not possible because I need to call that function multiple times. While trying to make it compile I arrived at two possibilities:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub enum Parsed&lt;'a, T&gt; {
      Some(T, &amp;'a str),
      None(&amp;'a str),
  }
  
  impl&lt;'a, T&gt; Parsed&lt;'a, T&gt; {
      pub fn unwrap(self) -&gt; (T, &amp;'a str) {
          match self {
              Parsed::Some(head, tail) =&gt; (head, &amp;tail),
              _ =&gt; panic!("Called unwrap on nothing."),
          }
      }
  
      pub fn is_none(&amp;self) -&gt; bool {
          match self {
              Parsed::None(_) =&gt; true,
              _ =&gt; false,
          }
      }
  }
  
  pub fn achar(character: char) -&gt; impl Fn(&amp;str) -&gt; Parsed&lt;char&gt; {
      move |input|
          match input.chars().next() {
              Some(c) if c == character =&gt; Parsed::Some(c, &amp;input[1..]),
              _ =&gt; Parsed::None(input),
          }
  }
  
  pub fn some_v1&lt;T&gt;(parser: impl Fn(&amp;str) -&gt; Parsed&lt;T&gt;) -&gt; impl Fn(&amp;str) -&gt; Parsed&lt;Vec&lt;T&gt;&gt; {
      move |input| {
          let mut re = Vec::new();
          let mut pos = input;
          loop {
              match parser(pos) {
                  Parsed::Some(head, tail) =&gt; {
                      re.push(head);
                      pos = tail;
                  }
                  Parsed::None(_) =&gt; break,
              }
          }
          Parsed::Some(re, pos)
      }
  }
  
  pub fn some_v2&lt;T&gt;(mut parser: impl FnMut(&amp;str) -&gt; Parsed&lt;T&gt;) -&gt; impl FnMut(&amp;str) -&gt; Parsed&lt;Vec&lt;T&gt;&gt; {
      move |input| {
          let mut re = Vec::new();
          let mut pos = input;
          loop {
              match parser(pos) {
                  Parsed::Some(head, tail) =&gt; {
                      re.push(head);
                      pos = tail;
                  }
                  Parsed::None(_) =&gt; break,
              }
          }
          Parsed::Some(re, pos)
      }
  }
  
  #[test]
  fn try_it() {
      assert_eq!(some_v1(achar('#'))("##comment").unwrap(), (vec!['#', '#'], "comment"));
      assert_eq!(some_v2(achar('#'))("##comment").unwrap(), (vec!['#', '#'], "comment"));
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=392f97408cd3c256417b7835856cae8f" rel="nofollow noreferrer">playground</a></p>
  
  <p>Now I don't know which version is to be preferred. Version 1 takes <code>Fn</code> which is less general, but version 2 needs its parameter mutable.</p>
  
  <p>Which one is more idiomatic/should be used and what is the rationale behind?</p>
  
  <hr>
  
  <p><strong>Update:</strong> Thanks <a href="https://stackoverflow.com/a/60272052/581002">jplatte</a> for the suggestion on version one. I updated the code here, that case I find even more interesting.</p>