Function cesu8::to_cesu8

source · []
pub fn to_cesu8(text: &str) -> Cow<'_, [u8]>
Expand description

Convert a Rust &str to CESU-8 bytes.

use std::borrow::Cow;
use cesu8::to_cesu8;

// This string is valid as UTF-8 or CESU-8, so it doesn't change,
// and we can convert it without allocating memory.
assert_eq!(Cow::Borrowed("aé日".as_bytes()), to_cesu8("aé日"));

// This string is a 4-byte UTF-8 string, which becomes a 6-byte CESU-8
// vector.
assert_eq!(Cow::Borrowed(&[0xED, 0xA0, 0x81, 0xED, 0xB0, 0x81]),
           to_cesu8("\u{10401}"));