Function cesu8::encode [−][src]
pub fn encode(str: &str) -> Cow<'_, [u8]>
Converts a string slice to CESU-8 bytes.
If the string slice’s representation in CESU-8 would be identical to its
present UTF-8 representation, this function is functionally no different
than (&str).as_bytes()
; this means that encode()
does not need to
perform any further operations and doesn’t need to allocate any additional
memory.
If the string slice’s representation in UTF-8 is not equivalent in CESU-8,
encode()
encodes the string slice to its CESU-8 representation as a slice
of bytes.
use std::borrow::Cow; use cesu8; let str = "Hello, world!"; // Since 'str' is valid UTF-8 and CESU-8 data, 'cesu8::encode' can encode // data without allocating memory. assert_eq!(cesu8::encode(str), Cow::Borrowed(str.as_bytes())); let utf8_data = "\u{10401}"; let cesu8_data = Cow::Borrowed(&[0xED, 0xA0, 0x81, 0xED, 0xB0, 0x81]); // 'utf8_data' is a 4-byte UTF-8 representation, which becomes a 6-byte // CESU-8 representation. assert_eq!(cesu8::encode(utf8_data), cesu8_data);