Last active
January 6, 2022 01:42
-
-
Save MikuroXina/d4a09fa80a2bc52a99aae7116f7d95bd to your computer and use it in GitHub Desktop.
The iterator for digits of a number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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