title: How to convert a Rust char to an integer so that '1' becomes 1?
body: <p>I am trying to find the sum of the digits of a given number. For example, <code>134</code> will give <code>8</code>.</p>
  
  <p>My plan is to convert the number into a string using <code>.to_string()</code> and then use <code>.chars()</code> to iterate over the digits as characters. Then I want to convert every <code>char</code> in the iteration into an integer and add it to a variable. I want to get the final value of this variable.</p>
  
  <p>I tried using the code below to convert a <code>char</code> into an integer:</p>
  
  <pre><code>fn main() {
      let x = "123";
      for y in x.chars() {
          let z = y.parse::&lt;i32&gt;().unwrap();
          println!("{}", z + 1);
      }
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=132027c6a92b20e5d39298ff2d3bdc24&amp;version=stable&amp;backtrace=0" rel="noreferrer">Playground</a>)</p>
  
  <p>But it results in this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0599]: no method named `parse` found for type `char` in the current scope
   --&gt; src/main.rs:4:19
    |
  4 |         let z = y.parse::&lt;i32&gt;().unwrap();
    |                   ^^^^^
  </code></pre>
  
  <p>This code does exactly what I want to do, but first I have to convert each <code>char</code> into a string and then into an integer to then increment <code>sum</code> by <code>z</code>.</p>
  
  <pre><code>fn main() {
      let mut sum = 0;
      let x = 123;
      let x = x.to_string();
      for y in x.chars() {
          // converting `y` to string and then to integer
          let z = (y.to_string()).parse::&lt;i32&gt;().unwrap();
          // incrementing `sum` by `z`
          sum += z;
      }
      println!("{}", sum);
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=8aae5aac5ead131f58633609f272a981&amp;version=stable&amp;backtrace=0" rel="noreferrer">Playground</a>)</p>