Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Last active January 6, 2022 01:42
Show Gist options
  • Select an option

  • Save MikuroXina/d4a09fa80a2bc52a99aae7116f7d95bd to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/d4a09fa80a2bc52a99aae7116f7d95bd to your computer and use it in GitHub Desktop.
The iterator for digits of a number.
pub struct Digits {
num: u64,
radix: u64,
}
impl Digits {
pub fn new(num: u64, radix: u64) -> Self {
assert!(0 < radix, "radix must be greater than zero");
Self { num, radix }
}
}
impl Iterator for Digits {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
if self.num == 0 {
None
} else {
let digit = self.num % self.radix;
self.num /= self.radix;
Some(digit)
}
}
}
impl std::iter::FusedIterator for Digits {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment