update doc examples
- Make examples runnable for doc tests - Add error handling using `?` instead of `unwrap`
This commit is contained in:
		| @@ -87,27 +87,31 @@ impl Response { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// Try and deserialize the response body as JSON. | ||||
|     /// Try and deserialize the response body as JSON using `serde`. | ||||
|     /// | ||||
|     /// # Examples | ||||
|     /// | ||||
|     /// ```rust,no_run | ||||
|     /// extern crate reqwest; | ||||
|     /// #[macro_use] | ||||
|     /// extern crate serde_derive; | ||||
|     /// | ||||
|     /// ```rust | ||||
|     /// # extern crate reqwest; | ||||
|     /// # #[macro_use] extern crate serde_derive; | ||||
|     /// # | ||||
|     /// # use reqwest::Error; | ||||
|     /// # | ||||
|     /// #[derive(Deserialize)] | ||||
|     /// struct User { | ||||
|     ///     name: String, | ||||
|     ///     age: u8, | ||||
|     /// struct Response { | ||||
|     ///     origin: String, | ||||
|     /// } | ||||
|     /// | ||||
|     /// fn main() { | ||||
|     ///     let user: User = reqwest::get("http://127.0.0.1/user.json") | ||||
|     ///         .expect("network error") | ||||
|     ///         .json() | ||||
|     ///         .expect("malformed json"); | ||||
|     /// } | ||||
|     /// # fn run() -> Result<(), Error> { | ||||
|     /// let resp: Response = reqwest::get("http://127.0.0.1/user.json")?.json()?; | ||||
|     /// # Ok(()) | ||||
|     /// # } | ||||
|     /// # | ||||
|     /// # fn main() { | ||||
|     /// #     if let Err(error) = run() { | ||||
|     /// #         println!("Error: {:?}", error); | ||||
|     /// #     } | ||||
|     /// # } | ||||
|     /// ``` | ||||
|     #[inline] | ||||
|     pub fn json<T: DeserializeOwned>(&mut self) -> ::Result<T> { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user