Number to vector of its digits There is a function that returns digits as vector of given number. Is it idiomatic way to solve this problem in Rust? Can you suggest more concise solution?   

#[allow(dead_code)]
fn num_digits(num: i32) -&gt; Vec&lt;i32&gt; {
    let mut x = num;
    let mut result: Vec&lt;i32&gt; = Vec::new();

    loop {
        result.push(x % 10);
        x /= 10;
        if x == 0 {break}
    }

    result.reverse();
    result
}

#[test]
fn digits() {
    assert_eq!(num_digits(0), [0]);
    assert_eq!(num_digits(-1), [-1]);
    assert_eq!(num_digits(-123), [-1, -2, -3]);
    assert_eq!(num_digits(1), [1]);
    assert_eq!(num_digits(12), [1, 2]);
    assert_eq!(num_digits(456464), [4, 5, 6, 4, 6, 4]);
}
I would call this "more idiomatic" but I'm still not satisfied with it. Rust uses Iterators in a lot of ways, e.g. look at this implementation:

pub fn num_digits(num: i32) -&gt; Vec&lt;i32&gt; {
    let mul = if num &lt; 0 { -1 } else { 1 };
    num.to_string()
        .chars()
        .filter_map(|x| x.to_digit(10))
        .map(|x| (x as i32) * mul)
        .collect()
}


The mul variable is just for determining if you want negative or positive numbers.

Because i32 implements the Display trait you can call to_string on it, which will give you a String representation of the i32.
Next is just iterating of the characters of the String and trying to convert them to a digit. - will be ignored, because it is not a digit and filter_map will filter all Results, that will return a Err. The next thing is to convert the numbers to a i32 (because to_digit gives an u32) and either making them negative if the original number was negative or staying positive.

collect will put all the numbers into a collection, in this case a Vec because the type can be deduced by Rust (because of the return type)