I've been looking for any working examples and good documentation for the hyper crate in rust and came across an example here. However, when I run that example I get several errors.
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
--> src/main.rs:8:18
|
8 | let client = Client::new();
| ^^^^^^^^^^^^^ expected 1 parameter
error[E0308]: mismatched types
--> src/main.rs:11:26
|
11 | let res = client.get("https://www.reddit.com/r/programming/.rss")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `hyper::Uri`, found reference
|
= note: expected type `hyper::Uri`
found type `&'static str`
error[E0599]: no method named `send` found for type `hyper::client::FutureResponse` in the current scope
--> src/main.rs:12:22
|
12 | .send()
| ^^^^
error: aborting due to 3 previous errors
Does anyone know what I'm doing wrong? I'm really struggling to understand how to make functional rust code when example code doesn't work for me.
Looks like that example is out of date, so taking the example from hyper documentation and adding fn main() so that it might work:
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;
fn main(){
let mut core = Core::new()?;
let client = Client::new(&core.handle());
let uri = "http://httpbin.org/ip".parse()?;
let work = client.get(uri).and_then(|res| {
println!("Response: {}", res.status());
res.body().for_each(|chunk| {
io::stdout()
.write_all(&chunk)
.map_err(From::from)
})
});
core.run(work)?;
}
Which fails with the errors:
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> src/main.rs:11:20
|
11 | let mut core = Core::new()?;
| ------------
| |
| the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> src/main.rs:14:15
|
14 | let uri = "http://httpbin.org/ip".parse()?;
| --------------------------------
| |
| the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> src/main.rs:24:5
|
24 | core.run(work)?;
| ---------------
| |
| the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error: aborting due to 3 previous errors
Thanks in advance.