trait-concept

DeserializeOwned generic bound

Use this bound when a result cannot borrow from input that will be destroyed inside the function. It does not require every field to allocate. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 2 Common mistake: Replacing the bound with Deserialize<'static> for a temporary input.

Formal Syntax & Structure

rust syntaxrust
T: serde::de::DeserializeOwned

Working Example

Example Snippetrust
fn main() {    fn load<T: serde::de::DeserializeOwned>(s: String) -> Result<T, serde_json::Error> {        serde_json::from_str(&s)    }    let value: Vec<String> = load(String::from(r#"["book","pen"]"#)).unwrap();    println!("{}", value.len());}
Rust Reference