rot13加密算法
1. pub fn rot13(text: &str) -> String {
2. let to_enc = text.to_uppercase();
3. to_enc
4. .chars()
5. .map(|c| match c {
6. 'A'..='M' => ((c as u8) + 13) as char,
7. 'N'..='Z' => ((c as u8) - 13) as char,
8. _ => c,
9. })
10. .collect()
11. }
13. #[cfg(test)]
14. mod test {
15. use super::*;
17. #[test]
18. fn test_single_letter() {
19. assert_eq!("N", rot13("A"));
20. }
22. #[test]
23. fn test_bunch_of_letters() {
24. assert_eq!("NOP", rot13("ABC"));
25. }
27. #[test]
28. fn test_non_ascii() {
29. assert_eq!("😀NO", rot13("😀AB"));
30. }
32. #[test]
33. fn test_twice() {
34. assert_eq!("ABCD", rot13(&rot13("ABCD")));
35. }
36. }