rot13第二种实现

1. pub fn another_rot13(text: &str) -> String {
2. let input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
3. let output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
4. text.chars()
5. .map(|c| match input.find(c) {
6. Some(i) => output.chars().nth(i).unwrap(),
7. None => c,
8. })
9. .collect()
10. }

12. #[cfg(test)]
13. mod tests {
14. // Note this useful idiom: importing names from outer (for mod tests) scope.
15. use super::*;

17. #[test]
18. fn test_simple() {
19. assert_eq!(another_rot13("ABCzyx"), "NOPmlk");
20. }

22. #[test]
23. fn test_every_alphabet_with_space() {
24. assert_eq!(
25. another_rot13("The quick brown fox jumps over the lazy dog"),
26. "Gur dhvpx oebja sbk whzcf bire gur ynml qbt"
27. );
28. }

30. #[test]
31. fn test_non_alphabet() {
32. assert_eq!(another_rot13("🎃 Jack-o'-lantern"), "🎃 Wnpx-b'-ynagrea");
33. }
34. }