Trait num_digitize::ToDigits
[−]
[src]
pub trait ToDigits where
Self: Copy + Clone + Num + NumCast + DivAssign, { fn to_digits(&self) -> Vec<i8> { ... } }
Provided Methods
fn to_digits(&self) -> Vec<i8>
Converts integer to Vec<i8>
of its digits (base 10).
Example
Basic usage:
use num_digitize::ToDigits; let number: u8 = 12; let vector: Vec<i8> = vec![1, 2]; assert_eq!(number.to_digits(), vector);
Negative numbers return all the digits negative Vec<u8>
:
use num_digitize::ToDigits; let number = -12; assert_eq!(number.to_digits(), vec![-1, -2]);
Reason for this is mathematically you can easily add these back to the original number like so:
-123 -> [-1, -2, -3]
(-1 * 10^2) + (-2 * 10^1) + (-3 * 10^0) = -123
Or with FromDigits
trait.