Created
May 31, 2018 22:07
-
-
Save killercup/2247b7e6431010122f0a779531a8ff89 to your computer and use it in GitHub Desktop.
Serde Cows (2 of 3)
This file contains 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" }"#; | |
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