Trait ToDigits

Source
pub trait ToDigits
where Self: Copy + Clone + Num + NumCast + DivAssign,
{ // Provided method fn to_digits(&self) -> Vec<i8> { ... } }

Provided Methods§

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToDigits for i8

Source§

impl ToDigits for i16

Source§

impl ToDigits for i32

Source§

impl ToDigits for i64

Source§

impl ToDigits for isize

Source§

impl ToDigits for u8

Source§

impl ToDigits for u16

Source§

impl ToDigits for u32

Source§

impl ToDigits for u64

Source§

impl ToDigits for usize

Implementors§