pub fn decode(bytes: &[u8]) -> Result<Cow<'_, str>, Error>Expand description
Converts a slice of bytes to a string slice.
First, if the slice of bytes is already valid UTF-8, this function is
functionally no different than std::str::from_utf8; this means that
decode() does not need to perform any further operations and doesn’t need
to allocate additional memory.
If the slice of bytes is not valid UTF-8, decode() works on the assumption
that the slice of bytes, if not valid UTF-8, is valid MUTF-8. It will then
decode the bytes given to it and return the newly constructed string slice.
If the slice of bytes is found not to be valid MUTF-8 data, decode()
returns Err(Error) to signify that an error has occurred.
§Errors
Returns Error if the input is invalid MUTF-8 data.
§Examples
use alloc::borrow::Cow;
let str = "Hello, world!";
// Since 'str' contains valid UTF-8 and MUTF-8 data, 'from_mutf8' can
// decode the string slice without allocating memory.
assert_eq!(mutf8::decode(str.as_bytes()), Ok(Cow::Borrowed(str)));
let str = "\u{10401}";
let mutf8_data = &[0xED, 0xA0, 0x81, 0xED, 0xB0, 0x81];
// 'mutf8_data' is a byte slice containing a 6-byte surrogate pair which
// becomes the 4-byte UTF-8 character 'str'.
assert_eq!(mutf8::decode(mutf8_data), Ok(Cow::Owned(str.to_string())));
let str = "\0";
let mutf8_data = &[0xC0, 0x80];
// 'mutf8_data' is a byte slice containing MUTF-8 data containing a null
// code point which becomes a null character.
assert_eq!(mutf8::decode(mutf8_data), Ok(Cow::Owned(str.to_string())));