gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:14:12 24 4
gpt4 key购买 nike

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

我找到的最接近的是 from_utf8这会将切片解释为 UTF8,但我不是在 UTF8 之后,而是文字代码点。

如何做到这一点?

最佳答案

fn main() {
let codepoint_array: Vec<u8> = "test".into();
let codepoints: Vec<char> = codepoint_array.into_iter().map(char::from).collect();
println!("{:?}", codepoints);
}

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

关于string - 如何在 Rust 中将 Unicode 代码点的 [u8] 转换为 str?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48025236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com