rot13第二种实现
pub fn another_rot13(text: &str) -> String {
let input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
text.chars()
.map(|c| match input.find(c) {
Some(i) => output.chars().nth(i).unwrap(),
None => c,
})
.collect()
}
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn test_simple() {
assert_eq!(another_rot13("ABCzyx"), "NOPmlk");
}
#[test]
fn test_every_alphabet_with_space() {
assert_eq!(
another_rot13("The quick brown fox jumps over the lazy dog"),
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt"
);
}
#[test]
fn test_non_alphabet() {
assert_eq!(another_rot13("🎃 Jack-o'-lantern"), "🎃 Wnpx-b'-ynagrea");
}
}
当前内容版权归 rustlang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rustlang-cn .