doc(client): show spawning a client future, clarify lazy in client example

This commit is contained in:
Sean McArthur
2018-06-18 17:02:57 -07:00
parent 482a5f589e
commit 61f31b5a4a
2 changed files with 72 additions and 61 deletions

View File

@@ -28,7 +28,14 @@ fn main() {
return; return;
} }
rt::run(rt::lazy(move || { // Run the runtime with the future trying to fetch and print this URL.
//
// Note that in more complicated use cases, the runtime should probably
// run on its own, and futures should just be spawned into it.
rt::run(fetch_url(url));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new(); let client = Client::new();
client client
@@ -55,5 +62,4 @@ fn main() {
.map_err(|err| { .map_err(|err| {
eprintln!("Error {}", err); eprintln!("Error {}", err);
}) })
}));
} }

View File

@@ -23,20 +23,22 @@
//! //!
//! ## Example //! ## Example
//! //!
//! ```no_run //! For a small example program simply fetching a URL, take a look at the
//! [full client example](https://github.com/hyperium/hyper/blob/master/examples/client.rs).
//!
//! ```
//! extern crate hyper; //! extern crate hyper;
//! //!
//! use hyper::Client; //! use hyper::Client;
//! # #[cfg(feature = "runtime")] //! # #[cfg(feature = "runtime")]
//! use hyper::rt::{self, lazy, Future, Stream}; //! use hyper::rt::{self, Future, Stream};
//! //!
//! # #[cfg(feature = "runtime")] //! # #[cfg(feature = "runtime")]
//! fn main() { //! # fn fetch_httpbin() {
//! // A runtime is needed to execute our asynchronous code.
//! rt::run(lazy(|| {
//! let client = Client::new(); //! let client = Client::new();
//! //!
//! client //! let fut = client
//!
//! // Make a GET /ip to 'http://httpbin.org' //! // Make a GET /ip to 'http://httpbin.org'
//! .get("http://httpbin.org/ip".parse().unwrap()) //! .get("http://httpbin.org/ip".parse().unwrap())
//! //!
@@ -65,10 +67,13 @@
//! // Map any errors that might have happened... //! // Map any errors that might have happened...
//! .map_err(|err| { //! .map_err(|err| {
//! println!("error: {}", err); //! println!("error: {}", err);
//! }) //! });
//! })); //!
//! } //! // A runtime is needed to execute our asynchronous code. In order to
//! # #[cfg(not(feature = "runtime"))] //! // spawn the future into the runtime, it should already have been
//! // started and running before calling this this code.
//! rt::spawn(fut);
//! # }
//! # fn main () {} //! # fn main () {}
//! ``` //! ```