Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple examples for async await intro slides #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions presentations/async-await-intro/1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async fn is_website_up(url: &str) -> Result<bool, Box<dyn Error + Send + Sync>> {
let url = url.parse::<hyper::Uri>()?;

let client = hyper::Client::new();
let res = client.get(url).await?;

let status_code = res.status();
Ok(status_code.is_success())
}
9 changes: 9 additions & 0 deletions presentations/async-await-intro/2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async fn fetch_into_string(url: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
let url: hyper::Uri = url.parse()?;

let client = hyper::Client::new();
let res = client.get(url).await?;

let bytes = hyper::body::to_bytes(res.into_body()).await?;
Ok(String::from_utf8(bytes.to_vec())?)
}
4 changes: 4 additions & 0 deletions presentations/async-await-intro/3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let check_example = is_website_up("http://example.com");
let check_forever = is_website_up("http://httpforever.com");

let (example_result, forever_result) = join!(check_example, check_forever);
6 changes: 6 additions & 0 deletions presentations/async-await-intro/4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select! {
example = fetch_into_string("http://example.com") =>
println!("Example is faster with body:\n{}", example?),
forever = fetch_into_string("http://httpforever.com") =>
println!("HttpForever is faster with body:\n{}", forever?),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading