Created
April 27, 2014 13:44
-
-
Save lordmauve/11345999 to your computer and use it in GitHub Desktop.
ROT13 stdin to stdout in Rust
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
use std::io; | |
// Get the ROT13ed equivalent of char | |
fn rot13(c: char) -> char { | |
let base = match c { | |
'a'..'z' => 'a' as u8, | |
'A'..'Z' => 'A' as u8, | |
_ => return c | |
}; | |
let ord = c as u8 - base; | |
let rot = (ord + 13) % 26; | |
(rot + base) as char | |
} | |
// ROT13 stdin and write to stdout | |
fn main() { | |
let mut reader = io::stdin(); | |
let mut output = io::stdout(); | |
for x in reader.chars() { | |
let res = output.write_char(rot13(x.unwrap())); | |
if res.is_err() { | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment