title: How do I use nom to parse a string with sign into an i32?
body: <p>Using the <a href="https://crates.io/crates/nom" rel="nofollow noreferrer">nom</a> crate, I'm trying to write a parser that can recognize signed <code>i32</code> number from a <code>String</code>, i.e. can transform the string <code>-42</code> into the <code>i32</code> representation.</p>
  
  <p>So far I've come up with the following, but I'm failing to parse negative numbers:</p>
  
  <pre><code>use nom::types::CompleteStr;
  use std::str::FromStr;
  
  named!(
      i32_parser&lt;CompleteStr, i32&gt;,
      map_res!(nom::digit, |CompleteStr(s)| i32::from_str(s))
  );
  
  #[test]
  fn parse_i32_positive() {
      assert_eq!(
          i32_parser(CompleteStr::from("42")),
          Ok((CompleteStr::from(""), 42))
      );
  }
  
  #[test]
  fn parse_i32_negative() {
      assert_eq!(
          i32_parser(CompleteStr::from("-42")),
          Ok((CompleteStr::from(""), -42))
      );
  }
  </code></pre>
  
  <p>I've also tried the following, but with a cryptic compilation error:</p>
  
  <pre class="lang-none prettyprint-override"><code>named!(
       i32_parser&lt;CompleteStr, i32&gt;,
       map_res!(alt!(char!('-') | nom::digit), |CompleteStr(s)| i32::from_str(s))
   );
  ^ expected char, found struct `nom::types::CompleteStr`
  </code></pre>
  
  <p>Any suggestion on how to fix it? Or a simpler way to achieve that with nom?</p>
  
  <p>I'm explicitly looking to implement this with nom because I'm trying to parse a more complex structure. <code>i32::from_str(s)</code> works for simple strings but it's not what I'm looking for.</p>