Convert to Vec<i32> to i32

77 Views Asked by At

I was trying to create a function that trasform a Vec to i32

fn vec_to_int(digits: Vec<i32>) -> i32 {
        let n = &digits.iter().fold(0, |acc, digit| acc * 10 + digit);
        println!("Vec:{:?} to: {}",digits,n);
        *n
    }

the results of this function was just as expected

Vec:[3, 4, 2] to: 342
Vec:[4, 6, 5] to: 465
Vec:[9] to: 9
Vec:[9, 9, 9, 9, 9, 9, 9] to: 9999999

but when i try using the vec[9, 9, 9, 9, 9, 9, 9, 9, 9, 1] the result was

Vec:[9, 9, 9, 9, 9, 9, 9, 9, 9, 1] to: 1410065399

why this happen and what i can do to improve the code?

I know that i could just parse the Vec to a string than parse to a number but i want to know why it happen and why exactly 1410065399

0

There are 0 best solutions below