borrowing

Borrowed deserialization from JSON text

Borrow text directly from input when the format and data permit it. Escaped JSON strings may need allocated storage; an &str field cannot borrow a decoded string that is not present contiguously in the source. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): book true Common mistake: Promising zero-copy for every valid JSON string.

Formal Syntax & Structure

rust syntaxrust
#[derive(serde::Deserialize)] struct Label<'a> { name: &'a str }

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Deserialize)]    struct Label<'a> { name: &'a str }    let input = String::from(r#"{"name":"book"}"#);    let v: Label<'_> = serde_json::from_str(&input).unwrap();    println!("{}", v.name);    println!("{}", serde_json::from_str::<Label<'_>>(r#"{"name":"b\u006fok"}"#).is_err());}
Rust Reference