4.7 练习:ROT13

练习:ROT13 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/std-traits/exercise.html

4.7 练习:ROT13

在本例中,你将实现经典的 “ROT13” 密码。把这段代码复制到 playground,并实现缺失部分。只旋转 ASCII 字母字符,以确保结果仍是有效的 UTF-8。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
use std::io::Read;

struct RotDecoder<R: Read> {
    input: R,
    rot: u8,
}

// 为 `RotDecoder` 实现 `Read` trait。

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn joke() {
        let mut rot =
            RotDecoder { input: "Gb trg gb gur bgure fvqr!".as_bytes(), rot: 13 };
        let mut result = String::new();
        rot.read_to_string(&mut result).unwrap();
        assert_eq!(&result, "To get to the other side!");
    }

    #[test]
    fn binary() {
        let input: Vec<u8> = (0..=255u8).collect();
        let mut rot = RotDecoder::<&[u8]> { input: input.as_slice(), rot: 13 };
        let mut buf = [0u8; 256];
        assert_eq!(rot.read(&mut buf).unwrap(), 256);
        for i in 0..=255 {
            if input[i] != buf[i] {
                assert!(input[i].is_ascii_alphabetic());
                assert!(buf[i].is_ascii_alphabetic());
            }
        }
    }
}

若把两个 RotDecoder 实例串起来,每个都旋转 13 个字符,会发生什么?


4.7.1 解答

01-解答 — Comprehensive Rust

最后修改 August 11, 2026: 更新 (70a5af133)