Skip to content

Instantly share code, notes, and snippets.

@iagox86
Created November 30, 2018 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iagox86/146f8426425a10eb882126b49152e98d to your computer and use it in GitHub Desktop.
Save iagox86/146f8426425a10eb882126b49152e98d to your computer and use it in GitHub Desktop.
pub fn from_binary(s: &str) -> Option<u32> {
let total: u32 = s.chars().rev().enumerate().map(|c| {
let (i, c) = c;
if c == '1' {
(2 as u32).pow(i as u32)
} else {
0
}
}).sum();
return Some(total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment