Skip to content

Instantly share code, notes, and snippets.

@killercup
Created May 31, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/c3997391e5bcb2834674c9c3e49e2f0c to your computer and use it in GitHub Desktop.
Save killercup/c3997391e5bcb2834674c9c3e49e2f0c to your computer and use it in GitHub Desktop.
Serde Cows (1 of 3)
#[macro_use] extern crate serde_derive;
extern crate serde_json;
use std::borrow::Cow;
#[derive(Debug, Deserialize)]
struct Foo<'input> {
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