Skip to content

Instantly share code, notes, and snippets.

@oal
Last active August 29, 2015 14:26
Show Gist options
  • Save oal/9ffce9d03fec4b005d6f to your computer and use it in GitHub Desktop.
Save oal/9ffce9d03fec4b005d6f to your computer and use it in GitHub Desktop.
Takes a number like 234908571 and returns the digits it contains as a Vec<u8>: [2, 3, 4, 9, 0, 8, 5, 7, 1]
fn digitize(n: u64) -> Vec<u8> {
let mut places = 0;
let mut ndiv = n;
while ndiv > 0 {
ndiv /= 10;
places += 1;
}
let mut digits = Vec::new();
let mut nsub = n;
for i in (0..places) {
let p = places-i-1;
let base = num::pow(10, p);
let mut digit = 0;
while nsub >= base {
nsub -= base;
digit += 1;
}
digits.push(digit);
}
digits
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment