I’m trying to convert a string example “12 34 56” into a vector of integers using Rust. I tried using .chunks() and a few other things the code I used works as expected for “123456” but both strings have the same output
[1, 2, 3, 4, 4, 5] I’m trying to get [12, 34, 56]
fn to_vector(s: &str) -> Vec<u32> {
let mut digits = Vec::new();
for c in s.chars() {
if let Some(digit) =c.to_digit(10) {
digits.push(digit);
}
}
digits
}
fn main() {
let my_string = "12 34 45";
let my_vec = to_vector(my_string);
println!("{:?}", my_vec);
}
New contributor
user28948807 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.