hyperium / hyperium.github.io

Home Page:http://hyper.rs

Repository from Github https://github.comhyperium/hyperium.github.ioRepository from Github https://github.comhyperium/hyperium.github.io

Cannot Compile HTTP Client Example in the Documentation

pyk opened this issue · comments

commented

I'm following this documentation and come up with the following code:

extern crate hyper;

use hyper::body::HttpBody as _;
use hyper::Client;
use tokio::io::{stdout, AsyncWriteExt as _};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // This is where we will setup our HTTP client requests.
    println!("Hello?");

    // Still inside `async fn main`...
    let client = Client::new();

    // Parse an `http::Uri`...
    let uri = "http://httpbin.org/ip".parse()?;

    // Await the response...
    let resp = client.get(uri).await?;

    println!("Response: {}", resp.status());

    // And now...
    while let Some(chunk) = resp.body_mut().data().await {
        stdout().write_all(&chunk?).await?;
    }

    Ok(())
}

When I try to compile and run the examples. I got the following error:

error[E0433]: failed to resolve: use of undeclared type or module `tokio`
 --> src\bin\hyper_get.rs:5:3
  |
5 | #[tokio::main]
  |   ^^^^^ use of undeclared type or module `tokio`

warning: unused import: `hyper::Client`
 --> src\bin\hyper_get.rs:3:5
  |
3 | use hyper::Client;
  |     ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0277]: `main` has invalid return type `impl std::future::Future`
 --> src\bin\hyper_get.rs:6:20
  |
PS C:\Users\bayu\pyk\rust-http-clients> cargo run --bin hyper_get
   Compiling rust-http-clients v0.1.0 (C:\Users\bayu\pyk\rust-http-clients)
error[E0433]: failed to resolve: use of undeclared type or module `tokio`
 --> src\bin\hyper_get.rs:5:5
  |
5 | use tokio::io::{stdout, AsyncWriteExt as _};
  |     ^^^^^ use of undeclared type or module `tokio`

error[E0433]: failed to resolve: use of undeclared type or module `tokio`
 --> src\bin\hyper_get.rs:7:3
  |
7 | #[tokio::main]
  |   ^^^^^ use of undeclared type or module `tokio`

error[E0425]: cannot find function `stdout` in this scope
  --> src\bin\hyper_get.rs:25:9
   |
25 |         stdout().write_all(&chunk?).await?;
   |         ^^^^^^ not found in this scope
   |
help: possible candidate is found in another module, you can import it into scope
   |
3  | use std::io::stdout;
   |

error[E0277]: `main` has invalid return type `impl std::future::Future`
 --> src\bin\hyper_get.rs:8:20
  |
8 | async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
  |
  = help: consider using `()`, or a `Result`

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0277, E0425, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `rust-http-clients`.

To learn more, run the command again with --verbose.