简体   繁体   中英

How to convert [u8] of Unicode code points to str in Rust?

I have a u8 slice that I would like to convert into a string, treating each u8 as the literal Unicode code point (that is, U+0000 to U+00FF).

The closest I've found is from_utf8 which would interpret the slice as UTF8, but I'm not after UTF8, but the literal code points instead.

How to do this?

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

(I have no idea why you'd want to do this, since that would give you a the Latin-1 supplement and the Latin Extended A, but nothing else...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM