Created
November 30, 2018 20:27
-
-
Save iagox86/b7eefb3d82ec7434b384b187e9898870 to your computer and use it in GitHub Desktop.
This file contains 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 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