如何将[u8]的Unicode代码转换为Rust中的str?

问题描述 投票:1回答:1

我有一个u8切片,我想将其转换为字符串,将每个u8视为文字Unicode代码点(即U + 0000到U + 00FF)。

我发现的最接近的是from_utf8,它将切片解释为UTF8,但我不是在UTF8之后,而是文字代码指向。

这该怎么做?

string rust
1个回答
3
投票
fn main() {
    let codepoint_array: Vec<u8> = "test".into();
    let codepoints: Vec<char> = codepoint_array.into_iter().map(char::from).collect();
    println!("{:?}", codepoints);
}

(我不知道你为什么要这样做,因为那会给你一个拉丁语1补充和拉丁语扩展A,但没有别的......)

© www.soinside.com 2019 - 2024. All rights reserved.