Skip to content

Instantly share code, notes, and snippets.

@misterhat
Last active August 29, 2015 14:00
Show Gist options
  • Save misterhat/5c98b7180bbc0c2ca296 to your computer and use it in GitHub Desktop.
Save misterhat/5c98b7180bbc0c2ca296 to your computer and use it in GitHub Desktop.
fn encode_username(username: &str) -> u64 {
let mut encoded = 0;
for character in username.chars() {
let rune = character.to_lowercase() as u64;
encoded *= 37;
encoded += rune - match rune {
97..122 => 96,
48..57 => 21,
_ => 0
};
}
encoded
}
fn encode_username(username: &str) -> u64 {
username.chars().fold(0, |encoded, character| {
let rune = character.to_lowercase() as u64;
(encoded * 37) + rune - match rune {
97..122 => 96,
48..57 => 21,
_ => 0
}
})
}
fn encode_username(username: &str) -> u64 {
username.chars().map(|c| c.to_lowercase() as u64).fold(0, |enc, c| {
(enc * 37) + c - match c {
97..122 => 96,
48..57 => 21,
_ => 0
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment