小编rad*_*iev的帖子

找不到将 char 转换为 u8 的方法

我一直在尝试将给定字符串切片中的每个字母替换为其在字母表中的位置(“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)

rust

5
推荐指数
1
解决办法
9663
查看次数

如何比较数字分组并返回最大的数字?

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)

string parsing compare numbers rust

0
推荐指数
1
解决办法
43
查看次数

标签 统计

rust ×2

compare ×1

numbers ×1

parsing ×1

string ×1