finish ch17.01
This commit is contained in:
parent
756e74c06a
commit
ba887b2e97
7
ch17/concurrency-with-async/Cargo.lock
generated
Normal file
7
ch17/concurrency-with-async/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "concurrency-with-async"
|
||||
version = "0.1.0"
|
6
ch17/concurrency-with-async/Cargo.toml
Normal file
6
ch17/concurrency-with-async/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "concurrency-with-async"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
6
ch17/concurrency-with-async/src/main.rs
Normal file
6
ch17/concurrency-with-async/src/main.rs
Normal file
@ -0,0 +1,6 @@
|
||||
//! Stopped here:
|
||||
//! https://rust-book.cs.brown.edu/ch17-02-concurrency-with-async.html
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
2121
ch17/futures-and-syntax/Cargo.lock
generated
2121
ch17/futures-and-syntax/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
trpl = "0.2.0"
|
||||
|
@ -5,11 +5,52 @@
|
||||
//! be ready at some point in the future.
|
||||
//!
|
||||
//! The `async` keyword can be applied to blocks and functions
|
||||
//! to specify they can be interrupted and resumed.
|
||||
//! to specify they can be interrupted and resumed. `await`
|
||||
//! keyword to wait for a future to become ready.
|
||||
//!
|
||||
//! Stopped here:
|
||||
//! https://rust-book.cs.brown.edu/ch17-01-futures-and-syntax.html#futures-and-the-async-syntax
|
||||
//! Rust handles these keywords differently from languages
|
||||
//! that have this feature already.
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::future::Future;
|
||||
use trpl::Html;
|
||||
|
||||
/// Writing `async fn` is equivalent to writing a function which
|
||||
/// returns a future of the return type.
|
||||
async fn page_title(url: &str) -> Option<String> {
|
||||
let response = trpl::get(url).await.text().await;
|
||||
Html::parse(&response_text)
|
||||
.select_first("title")
|
||||
.map(|title_element| title_element.inner_html())
|
||||
}
|
||||
|
||||
/// This function is equivalent to the function above.
|
||||
fn page_title(url: &str) -> impl Future<Output = Option<String>> + '_ {
|
||||
async move {
|
||||
let text = trpl::get(url).await.text().await;
|
||||
Html::parse(&text)
|
||||
.select_first("title")
|
||||
.map(|title| title.inner_html())
|
||||
}
|
||||
}
|
||||
|
||||
/// Rust doesn't allow for main to be async
|
||||
/// This can't happen because async code needs a runtime: a Rust crate
|
||||
/// which manages the details of executing async code. `main` can
|
||||
/// initialize a runime but isn't a runtime itself.
|
||||
///
|
||||
/// There are also different versions of async runtimes tailored to
|
||||
/// specific needs. For example, a high-throughput web server with many
|
||||
/// different CPU cores and a lot of RAM has different needs than a
|
||||
/// microcontroller with 1 core, a small amount of RAM, and no ability
|
||||
/// to do heap allocations.
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
trpl::run(async {
|
||||
let url = &args[1];
|
||||
match page_title(url).await {
|
||||
Some(title) => println!("The title for {url} is {title}"),
|
||||
None => println!("{url} had no title"),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user