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. }
  12. #[cfg(test)]
  13. mod test {
  14. use super::*;
  15. #[test]
  16. fn test_single_letter() {
  17. assert_eq!("N", rot13("A"));
  18. }
  19. #[test]
  20. fn test_bunch_of_letters() {
  21. assert_eq!("NOP", rot13("ABC"));
  22. }
  23. #[test]
  24. fn test_non_ascii() {
  25. assert_eq!("😀NO", rot13("😀AB"));
  26. }
  27. #[test]
  28. fn test_twice() {
  29. assert_eq!("ABCD", rot13(&rot13("ABCD")));
  30. }
  31. }