Skip to content

Commit 67c4781

Browse files
liufuyangseanmonstar
authored andcommitted
docs(examples): Update the example hello (#1852)
1 parent 8f4b05a commit 67c4781

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

examples/hello.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ extern crate pretty_env_logger;
55

66
use hyper::{Body, Request, Response, Server};
77
use hyper::service::{make_service_fn, service_fn};
8-
use hyper::rt;
98

109
async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
1110
Ok(Response::new(Body::from("Hello World!")))
1211
}
1312

14-
async fn serve() {
13+
#[tokio::main]
14+
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
pretty_env_logger::init();
16+
1517
let addr = ([127, 0, 0, 1], 3000).into();
1618

1719
let server = Server::bind(&addr)
1820
.serve(make_service_fn(|_| {
1921
// This is the `Service` that will handle the connection.
20-
// `service_fn_ok` is a helper to convert a function that
22+
// `service_fn` is a helper to convert a function that
2123
// returns a Response into a `Service`.
2224
async {
2325
Ok::<_, hyper::Error>(service_fn(hello))
@@ -26,13 +28,7 @@ async fn serve() {
2628

2729
println!("Listening on http://{}", addr);
2830

29-
if let Err(e) = server.await {
30-
eprintln!("server error: {}", e);
31-
}
32-
}
33-
34-
fn main() {
35-
pretty_env_logger::init();
31+
server.await?;
3632

37-
rt::run(serve());
33+
Ok(())
3834
}

0 commit comments

Comments
 (0)