Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Rust char to an integer so that '1' becomes 1?

Tags:

rust

I am trying to find the sum of the digits of a given number. For example, 134 will give 8.

My plan is to convert the number into a string using .to_string() and then use .chars() to iterate over the digits as characters. Then I want to convert every char in the iteration into an integer and add it to a variable. I want to get the final value of this variable.

I tried using the code below to convert a char into an integer:

fn main() {
    let x = "123";
    for y in x.chars() {
        let z = y.parse::<i32>().unwrap();
        println!("{}", z + 1);
    }
}

(Playground)

But it results in this error:

error[E0599]: no method named `parse` found for type `char` in the current scope
 --> src/main.rs:4:19
  |
4 |         let z = y.parse::<i32>().unwrap();
  |                   ^^^^^

This code does exactly what I want to do, but first I have to convert each char into a string and then into an integer to then increment sum by z.

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::<i32>().unwrap();
        // incrementing `sum` by `z`
        sum += z;
    }
    println!("{}", sum);
}

(Playground)

like image 419
ritiek Avatar asked May 15 '17 15:05

ritiek


People also ask

How do you convert char to int in Rust?

To convert a single char to an integer in Rust, use . to_digit(RADIX) . The radix value is used for conversion, 10 for decimal, 16 for hexadecimal.

Can you convert a character to an integer?

In Java, we can convert the Char to Int using different approaches. If we direct assign char variable to int, it will return the ASCII value of a given character. If the char variable contains an int value, we can get the int value by calling Character.

How do you convert digits in a string from character to integer?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

Which process convert the characters to integers?

Java char to int – implicit type casting A simple assignment of char value to an int variable would do the trick, compiler automatically convert the char to int, this process is known as implicit type casting or type promotion.


4 Answers

The method you need is char::to_digit. It converts char to a number it represents in the given radix.

You can also use Iterator::sum to calculate sum of a sequence conveniently:

fn main() {
    const RADIX: u32 = 10;
    let x = "134";
    println!("{}", x.chars().map(|c| c.to_digit(RADIX).unwrap()).sum::<u32>());
}
like image 195
Pavel Strakhov Avatar answered Oct 04 '22 18:10

Pavel Strakhov


my_char as u32 - '0' as u32

Now, there's a lot more to unpack about this answer.

It works because the ASCII (and thus UTF-8) encodings have the Arabic numerals 0-9 ordered in ascending order. You can get the scalar values and subtract them.

However, what should it do for values outside this range? What happens if you provide 'p'? It returns 64. What about '.'? This will panic. And '♥' will return 9781.

Strings are not just bags of bytes. They are UTF-8 encoded and you cannot just ignore that fact. Every char can hold any Unicode scalar value.

That's why strings are the wrong abstraction for the problem.

From an efficiency perspective, allocating a string seems inefficient. Rosetta Code has an example of using an iterator which only does numeric operations:

struct DigitIter(usize, usize);

impl Iterator for DigitIter {
    type Item = usize;
    fn next(&mut self) -> Option<Self::Item> {
        if self.0 == 0 {
            None
        } else {
            let ret = self.0 % self.1;
            self.0 /= self.1;
            Some(ret)
        }
    }
}

fn main() {
    println!("{}", DigitIter(1234, 10).sum::<usize>());
}
like image 26
Shepmaster Avatar answered Oct 04 '22 17:10

Shepmaster


Another way is to iterate over the characters of your string and convert and add them using fold.

fn sum_of_string(s: &str) -> u32 {
  s.chars().fold(0, |acc, c| c.to_digit(10).unwrap_or(0) + acc)
}

fn main() {
    let x = "123";
    println!("{}", sum_of_string(x));
}
like image 28
manonthemat Avatar answered Oct 04 '22 18:10

manonthemat


If c is your character you can just write:

c as i32 - 0x30;

Test with:

let c:char = '2';
let n:i32 = c as i32 - 0x30;
println!("{}", n);

output:

2

NB: 0x30 is '0' in ASCII table, easy enough to remember!

like image 31
Antonin GAVREL Avatar answered Oct 04 '22 17:10

Antonin GAVREL