Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1052

The from_utf8 Rust function cannot read some ASCII strings (invalid utf-8 sequence of 1 bytes)

$
0
0

I am trying to convert a vector of ASCII bytes into a rust string. I found the std::str::from_utf8() function, that should be able to handle all ASCII strings. For some reason it cannot read the copyright symbol, as shown in this code sample:

let buf = vec![0xA9, 0x41, 0x52, 0x54]; //©ARTprintln!("{}",    match std::str::from_utf8(&buf) {        Ok(x) => x,        Err(x) => {            println!("ERROR: {}", x);"failed"        }    });// > ERROR: invalid utf-8 sequence of 1 bytes from index 0

According to https://www.ascii-code.com/CP1252/1690xA9 is a valid ASCII character, and according to https://www.compart.com/en/unicode/U+00A9 also a valid UTF-8 character.

I also tried String::from_utf8_lossy(), but that gave me �ART as a result, which is not what the string should be.

Am I missing something here or is this a bug with the way rust handles ASCII?


Viewing all articles
Browse latest Browse all 1052

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>