Skip to content

Instantly share code, notes, and snippets.

@iagox86
Created November 30, 2018 20:27
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/b7eefb3d82ec7434b384b187e9898870 to your computer and use it in GitHub Desktop.
Save iagox86/b7eefb3d82ec7434b384b187e9898870 to your computer and use it in GitHub Desktop.
pub fn from_binary(s: &str) -> Option<u32> {
s.chars().rev().enumerate().try_fold(0, |acc, (i, c)| {
match c {
'1' => Some(acc + 2u32.pow(i as u32)),
'0' => Some(acc),
_ => None,
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment