我一直在尝试将给定字符串切片中的每个字母替换为其在字母表中的位置(“A”替换为“1”,“B”替换为“2”等)。
fn alphabet_position(text: &str) -> String {
let s = text
.chars()
.into_iter()
.filter(|&c| c.is_alphabetic())
.map(|c| c.to_ascii_uppercase())
.map(|c| c as u8)
.map(|c| (c - 64u8) as char)
.collect();
s
}
Run Code Online (Sandbox Code Playgroud) fn solve(s: &str) -> u32 {
s.split(char::is_alphabetic)
.map(|num| num.parse::<u32>().unwrap())
.max()
.unwrap()
}
fn main() {
assert_eq!(solve("gh12cdy695m1"), 695);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我收到这个错误:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: Empty }', src/main.rs:3:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Run Code Online (Sandbox Code Playgroud)