[−][src]Trait radixal::IntoDigits
An extension trait on unsigned integer types (u8
, u16
, u32
, u64
, u128
and usize
)
and the corresponding Wrapping
type.
Provided methods
fn into_digits(self, radix: Self) -> Result<DigitsIterator<Self>, RadixError>
Creates a DigitsIterator
with a given radix
.
Returns Err(RadixError)
if the radix is 0 or 1.
Example
use radixal::IntoDigits; let mut digits = 123_u32.into_digits(10).unwrap(); assert_eq!(digits.next(), Some(1)); assert_eq!(digits.next(), Some(2)); assert_eq!(digits.next(), Some(3)); assert_eq!(digits.next(), None);
ⓘImportant traits for DigitsIterator<T>fn into_binary_digits(self) -> DigitsIterator<Self>
Creates a DigitsIterator
with a binary radix.
Example
use radixal::IntoDigits; let mut digits = 12_u32.into_binary_digits(); assert_eq!(digits.next(), Some(1)); assert_eq!(digits.next(), Some(1)); assert_eq!(digits.next(), Some(0)); assert_eq!(digits.next(), Some(0)); assert_eq!(digits.next(), None);
ⓘImportant traits for DigitsIterator<T>fn into_decimal_digits(self) -> DigitsIterator<Self>
Creates a DigitsIterator
with a decimal radix.
Example
use radixal::IntoDigits; let mut digits = 12_u32.into_decimal_digits(); assert_eq!(digits.next(), Some(1)); assert_eq!(digits.next(), Some(2)); assert_eq!(digits.next(), None);
fn nbr_digits(self, radix: Self) -> Result<usize, RadixError>
Counts the number of digits for a given radix
.
Returns Err(RadixError)
if the radix is 0 or 1.
Example
use radixal::IntoDigits; let n = 123_u32; assert_eq!(n.nbr_digits(10).unwrap(), 3);
fn nbr_binary_digits(self) -> usize
Counts the number of binary digits.
Example
use radixal::IntoDigits; let n = 12_u32; assert_eq!(n.nbr_binary_digits(), 4);
fn nbr_decimal_digits(self) -> usize
Counts the number of decimal digits.
Example
use radixal::IntoDigits; let n = 123_u32; assert_eq!(n.nbr_decimal_digits(), 3);
fn is_palindrome(self, radix: Self) -> Result<bool, RadixError>
Checks if it is a palindrome for a given radix
.
Returns Err(RadixError)
if the radix is 0 or 1.
Example
use radixal::IntoDigits; let n = 123_u32; assert!(!n.is_palindrome(10).unwrap()); let n = 121_u32; assert!(n.is_palindrome(10).unwrap());
fn is_binary_palindrome(self) -> bool
Checks if it is a palindrome under a binary number system.
Example
use radixal::IntoDigits; let n = 12_u32; assert!(!n.is_binary_palindrome()); let n = 9_u32; assert!(n.is_binary_palindrome());
fn is_decimal_palindrome(self) -> bool
Checks if it is a palindrome under a decimal number system.
Example
use radixal::IntoDigits; let n = 123_u32; assert!(!n.is_decimal_palindrome()); let n = 121_u32; assert!(n.is_decimal_palindrome());
fn reverse_digits(self, radix: Self) -> Result<Self, RadixError>
Reverses the digits, returning a new number with the digits reversed, using wrapping semantics if necessary.
Since trailing zeroes are not conserved, this operation is not reversible.
Returns Err(RadixError)
if the radix is 0 or 1.
Example
use radixal::IntoDigits; let n = 123_u32; let reversed = n.reverse_digits(10).unwrap(); assert_eq!(reversed, 321); /// Wrapping on overflow. let n = 255_u8; let reversed = n.reverse_digits(10).unwrap(); assert_ne!(reversed, n); assert_eq!(reversed, 40); /// Trailing zeroes lead to irreversibility. let n = 1230_u32; let twice_reversed = n.reverse_digits(10).unwrap().reverse_digits(10).unwrap(); assert_ne!(twice_reversed, n); assert_eq!(twice_reversed, 123);
fn reverse_binary_digits(self) -> Self
Reverse the digits under a binary number system.
Since trailing zeroes are not conserved, this operation is not reversible.
Example
use radixal::IntoDigits; let n = 0b1100_u32; let m = n.reverse_binary_digits(); assert_eq!(m, 0b11);
fn reverse_decimal_digits(self) -> Self
Reverses the digits under a decimal number system, using wrapping semantics if necessary.
Since trailing zeroes are not conserved, this operation is not reversible.
Example
use radixal::IntoDigits; let n = 123_u32; let reversed = n.reverse_decimal_digits(); assert_eq!(reversed, 321);
fn is_permutation(self, other: Self, radix: Self) -> Result<bool, RadixError>
Tests if self
and other
are composed of the same digits under a given radix.
Since any number can be left-padded with 0
's, these are ignored when doing the
comparison.
Example
use radixal::IntoDigits; let n = 120_u32; let m = 2100_u32; assert!(n.is_permutation(m, 10).unwrap()); let n = 121_u32; let m = 2100_u32; assert!(!n.is_permutation(m, 10).unwrap());
fn is_decimal_permutation(self, other: Self) -> bool
Tests if self
and other
are composed of the same digits under a decimal radix.
Since any number can be left-padded with 0
's, these are ignored when doing the
comparison.
Example
use radixal::IntoDigits; let n = 120_u32; let m = 2100_u32; assert!(n.is_decimal_permutation(m)); let n = 121_u32; let m = 2100_u32; assert!(!n.is_decimal_permutation(m));
fn is_binary_permutation(self, other: Self) -> bool
Tests if self
and other
are composed of the same digits under a binary radix.
Since any number can be left-padded with 0
's, these are ignored when doing the
comparison.
This convenience function is offered for the sake of completeness; consider using the
count_ones
method instead.
Example
use radixal::IntoDigits; let n = 12_u32; let m = 17_u32; assert!(n.is_binary_permutation(m)); let n = 12_u32; let m = 7_u32; assert!(!n.is_binary_permutation(m));