RosettaCodeData/Task/Rot-13/Rust/rot-13-1.rust

17 lines
412 B
Plaintext

fn rot13 (string: String) -> String {
let mut bytes: Vec<u8> = string.into();
for byte in &mut bytes {
match *byte {
b'a'...b'm' | b'A'...b'M' => *byte += 13,
b'n'...b'z' | b'N'...b'Z' => *byte -= 13,
_ => (), // do nothing
}
}
String::from_utf8(bytes).unwrap()
}
fn main () {
let a = rot13("abc".to_owned());
assert_eq!(a, "nop");
}