Is it possible to decode bytes to UTF-8, converting errors to escape sequences in Rust?

前端 未结 2 416
我寻月下人不归
我寻月下人不归 2021-01-12 05:56

In Rust it\'s possible to get UTF-8 from bytes by doing this:

if let Ok(s) = str::from_utf8(some_u8_slice) {
    println!(\"example {}\", s);
}
2条回答
  •  不要未来只要你来
    2021-01-12 06:19

    You can either:

    1. Construct it yourself by using the strict UTF-8 decoding which returns an error indicating the position where the decoding failed, which you can then escape. But that's inefficient since you will decode each failed attempt twice.

    2. Try 3rd party crates which provide more customizable charset decoders.

提交回复
热议问题