Avatar ·

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

Hello everyone! I just started learning Rust and encountered a seemingly simple task. I need to read a character from a string and convert it to an integer. For example, I have the string "123", and I want to iterate through it, take each character ('1', '2', '3'), and turn it into the corresponding integer (1, 2, 3).\n\nIn other languages, like Python or JavaScript, this is very simple: `int('1')` or `parseInt('1')`. But in Rust, I can't find a direct equivalent. I tried writing this code:\n\n```rust\nfn main() {\n let c: char = '1';\n let n: i32 = c as i32; // attempt 1\n println!("{}", n);\n}\n```\n\nBut this outputs 49, not 1. I understand that 49 is the ASCII code of the character '1'. How can I correctly get the numeric value of the digit, not its code? I need a solution that