I'm learning Rust and am messing around with conversions of types because I need it for my first program.
Basically I'm trying to convert a singular string of numbers into an array of numbers.
eg. "609" -> [6,0,9]
const RADIX: u32 = 10;
let lines: Vec<String> = read_lines(filename);
let nums = lines[0].chars().map(|c| c.to_digit(RADIX).expect("conversion error"));
println!("Line: {:?}, Converted: {:?}", lines[0], nums);
I tried the above and the output is as follows:
Line: "603", Converted: Map { iter: Chars(['6', '0', '3']) }
Which I assume isn't correct. I'd need it to be just a pure array of integers so I can perform operations with it later.
Map. Only when you callcollect, this is when the iteration actually occurs.RADIX, because the string "609" is already in base 10. If you wanted to gets the digits in an other base, you'd have to convert that string to a number first, and then get the digits.