std-method

String::from_utf16

Validate UTF-16 code units and produce owned UTF-8 text. An isolated surrogate produces an error. Expected stdout (not executed): AπŸ˜€ true Common mistake: Passing an arbitrary byte buffer as UTF-16 code units.

Formal Syntax & Structure

rust syntaxrust
fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error>

Working Example

Example Snippetrust
fn main() {    println!("{}", String::from_utf16(&[0x41, 0xD83D, 0xDE00]).unwrap());    println!("{}", String::from_utf16(&[0xD800]).is_err());}
Rust Reference