title: Recognize floating point from input with nom
body: <p>I'm trying to use <a href="https://github.com/Geal/nom" rel="nofollow">nom</a> to parse a text based protocol. This protocol can have floating point values in it of the form:</p>
  
  <pre><code>[-]digit+[.digit+]
  </code></pre>
  
  <p>Examples of which are:</p>
  
  <ul>
  <li>-10.0</li>
  <li>10.0</li>
  <li>10</li>
  </ul>
  
  <p>The nom parser I've built to recognize this is... not pretty. It also doesn't quite typecheck. What I've got so far:</p>
  
  <pre><code>named!(float_prs &lt;&amp;[u8], f64&gt;,
         alt!(
             take_while!(nom::is_digit) =&gt; {|x| FromStr::from_str(std::str::from_utf8(x).unwrap()).unwrap()} |
             recognize!(chain!(
                 take_while!(nom::is_digit) ~
                     tag!(".") ~
                     take_while!(nom::is_digit),
                 || {})
             ) =&gt; {|x: &amp;[u8]| FromStr::from_str(std::str::from_utf8(x).unwrap()).unwrap() }
         )
  );
  </code></pre>
  
  <p>The first alternative parser recognizes <code>digit+</code>, the second is an attempt to recognize <code>digit+.digit+</code> but</p>
  
  <pre><code>&lt;nom macros&gt;:5:38: 5:62 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
  &lt;nom macros&gt;:5 let index = ( $ i ) . offset ( i ) ; $ crate:: IResult:: Done (
  </code></pre>
  
  <p>Recognizing <code>-digit</code> etc is not addressed in the above. There's a significant amount of duplication and the intent is very much obscured. Is there a better way to parse <code>[-]digit+[.digit+]</code> that I'm not seeing? </p>