std-method

String::from_utf8

Consume a byte vector, validating it before creating owned UTF-8 text. On error the original bytes can be recovered with into_bytes. Expected stdout (not executed): Aش Common mistake: Using unchecked conversion on unknown data.

Formal Syntax & Structure

rust syntaxrust
fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error>

Working Example

Example Snippetrust
fn main() {    let bytes = vec![65, 216, 180];    let text = String::from_utf8(bytes).unwrap();    println!("{text}");}
Rust Reference