Created
May 31, 2018 22:07
-
-
Save killercup/31491f2a3e9124f61d03972c9a1dad39 to your computer and use it in GitHub Desktop.
Serde Cows (3 of 3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate serde_derive; | |
extern crate serde_json; | |
use std::borrow::Cow; | |
#[derive(Debug, Deserialize)] | |
struct Foo<'input> { | |
#[serde(borrow)] | |
bar: Cow<'input, str>, | |
} | |
fn main() { | |
let input = r#"{ "bar": "\"baz?\" - \"yeah\"" }"#; | |
let foo: Foo = serde_json::from_str(input).unwrap(); | |
match foo.bar { | |
Cow::Borrowed(x) => println!("borrowed {}", x), | |
Cow::Owned(x) => println!("owned {}", x), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment