std-function

str::from_utf8 — validate borrowed bytes

Validate a byte slice and borrow it as text if the UTF-8 encoding is well formed. No owned String is required. Expected stdout (not executed): Aش true Common mistake: Assuming arbitrary binary data is valid text.

Formal Syntax & Structure

rust syntaxrust
fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error>

Working Example

Example Snippetrust
fn main() {    let good = [65, 216, 180];    let bad = [65, 255];    println!("{}", std::str::from_utf8(&good).unwrap());    println!("{}", std::str::from_utf8(&bad).is_err());}
Rust Reference