The Algorithms logo
The Algorithms
Acerca deDonar

Another Rot 13

pub fn another_rot13(text: &str) -> String {
    let input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    let output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
        .map(|c| match input.find(c) {
            Some(i) => output.chars().nth(i).unwrap(),
            None => c,

mod tests {
    // Note this useful idiom: importing names from outer (for mod tests) scope.
    use super::*;

    fn test_simple() {
        assert_eq!(another_rot13("ABCzyx"), "NOPmlk");

    fn test_every_alphabet_with_space() {
            another_rot13("The quick brown fox jumps over the lazy dog"),
            "Gur dhvpx oebja sbk whzcf bire gur ynml qbt"

    fn test_non_alphabet() {
        assert_eq!(another_rot13("馃巸 Jack-o'-lantern"), "馃巸 Wnpx-b'-ynagrea");