我一直在尝试将给定字符串切片中的每个字母替换为其在字母表中的位置(“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
}
最佳答案
试试 u8::to_string()
功能。
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).to_string())
.collect();
s
}
关于rust - 找不到将 char 转换为 u8 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66576556/