Skip to content

Instantly share code, notes, and snippets.

@lordmauve
Created April 27, 2014 13:44
Show Gist options
  • Save lordmauve/11345999 to your computer and use it in GitHub Desktop.
Save lordmauve/11345999 to your computer and use it in GitHub Desktop.
ROT13 stdin to stdout in Rust
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