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

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>());
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)