// ------------------------------------------------------------
// digits_to_number_join
// Converts digits to strings, concatenates, then parses.
// Example: [1,2,3,4] → "1234" → 1234
// ------------------------------------------------------------
fn digits_to_number_join(digits: &[i32]) -> i32 {
let s: String = digits.iter()
.map(|d| d.to_string())
.collect();
s.parse::<i32>().unwrap()
}
// ------------------------------------------------------------
// digits_to_number_math
// Pure mathematical folding (no string operations).
// Example: [1,2,3,4] → (((1*10)+2)*10+3)*10+4
// ------------------------------------------------------------
fn digits_to_number_math(digits: &[i32]) -> i32 {
let mut n = 0;
for d in digits {
n = n * 10 + d;
}
n
}
fn main() {
let digits = vec![4, 6, 3, 9, 1, 2];
println!("Using join(): {}", digits_to_number_join(&digits));
println!("Using math(): {}", digits_to_number_math(&digits));
}
/*
run:
Using join(): 463912
Using math(): 463912
*/