Vec<u8> to string

Example from documentation
https://doc.rust-lang.org/book/ch08-02-strings.html

    fn main() {
        for b in "Зд".bytes() {
            println!("{b}");
        }
    }

return the four bytes that make up this string

208
151
208
180

How to get back?

like this

    fn main() {
          
        let u: Vec<u8> = vec![208, 151, 208, 180];
        println!("u = {:?}", std::str::from_utf8 (&u[..]).unwrap()); // ? not work
    }

But if the string is without Cyrillic (for example "my text"), it works:

    fn main() {
          
        let u: Vec<u8> = vec![109, 121, 32, 116, 101, 120, 116];
        println!("u = {:?}", std::str::from_utf8 (&u[..]).unwrap()); // my text
    }

how to fix for Cyrillic and different data, thanks.

How does your first code not work? When I run it in the playground, I get the output:

u = "Зд"

which looks fine to me.

3 Likes

yes, strange, it worked too
looks like the problem was in play.rust ...
thanks

please, my question is here _ https://users.rust-lang.org

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.