mirror of https://github.com/KaiserY/trpl-zh-cn
parent
e675c74913
commit
a6dff2e316
@ -0,0 +1,6 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "ownership"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "ownership"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
@ -0,0 +1,8 @@
|
|||||||
|
fn main() {
|
||||||
|
// ANCHOR: here
|
||||||
|
let mut s = String::from("hello");
|
||||||
|
s = String::from("ahoy");
|
||||||
|
|
||||||
|
println!("{s}, world!");
|
||||||
|
// ANCHOR_END: here
|
||||||
|
}
|
@ -1,10 +1,22 @@
|
|||||||
#[test]
|
pub fn add(left: usize, right: usize) -> usize {
|
||||||
fn it_works() {
|
left + right
|
||||||
assert_eq!(2 + 2, 4);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
// ANCHOR: here
|
||||||
#[ignore]
|
#[cfg(test)]
|
||||||
fn expensive_test() {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = add(2, 2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn expensive_test() {
|
||||||
// 需要运行一个小时的代码
|
// 需要运行一个小时的代码
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// ANCHOR_END: here
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use adder;
|
use adder::add_two;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_adds_two() {
|
fn it_adds_two() {
|
||||||
assert_eq!(4, adder::add_two(2));
|
let result = add_two(2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
use adder;
|
use adder::add_two;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_adds_two() {
|
fn it_adds_two() {
|
||||||
common::setup();
|
common::setup();
|
||||||
assert_eq!(4, adder::add_two(2));
|
|
||||||
|
let result = add_two(2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use adder;
|
use adder::add_two;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_adds_two() {
|
fn it_adds_two() {
|
||||||
assert_eq!(4, adder::add_two(2));
|
let result = add_two(2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "add_one"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,17 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// TODO: we'll add this next!
|
||||||
|
}
|
||||||
|
|
||||||
|
// ANCHOR: all
|
||||||
|
use trpl::Html;
|
||||||
|
|
||||||
|
async fn page_title(url: &str) -> Option<String> {
|
||||||
|
let response = trpl::get(url).await;
|
||||||
|
let response_text = response.text().await;
|
||||||
|
Html::parse(&response_text)
|
||||||
|
.select_first("title")
|
||||||
|
.map(|title_element| title_element.inner_html())
|
||||||
|
}
|
||||||
|
// ANCHOR_END: all
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,16 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use trpl::Html;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// TODO: we'll add this next!
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn page_title(url: &str) -> Option<String> {
|
||||||
|
// ANCHOR: chaining
|
||||||
|
let response_text = trpl::get(url).await.text().await;
|
||||||
|
// ANCHOR_END: chaining
|
||||||
|
Html::parse(&response_text)
|
||||||
|
.select_first("title")
|
||||||
|
.map(|title_element| title_element.inner_html())
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,21 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use trpl::Html;
|
||||||
|
|
||||||
|
// ANCHOR: main
|
||||||
|
async fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
let url = &args[1];
|
||||||
|
match page_title(url).await {
|
||||||
|
Some(title) => println!("The title for {url} was {title}"),
|
||||||
|
None => println!("{url} had no title"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ANCHOR_END: main
|
||||||
|
|
||||||
|
async fn page_title(url: &str) -> Option<String> {
|
||||||
|
let response_text = trpl::get(url).await.text().await;
|
||||||
|
Html::parse(&response_text)
|
||||||
|
.select_first("title")
|
||||||
|
.map(|title_element| title_element.inner_html())
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,3 @@
|
|||||||
|
$ cargo run "http://www.rust-lang.org"
|
||||||
|
The title for http://www.rust-lang.org was
|
||||||
|
Rust Programming Language
|
@ -0,0 +1,24 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use trpl::Html;
|
||||||
|
|
||||||
|
// ANCHOR: run
|
||||||
|
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} was {title}"),
|
||||||
|
None => println!("{url} had no title"),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// ANCHOR_END: run
|
||||||
|
|
||||||
|
async fn page_title(url: &str) -> Option<String> {
|
||||||
|
let response_text = trpl::get(url).await.text().await;
|
||||||
|
Html::parse(&response_text)
|
||||||
|
.select_first("title")
|
||||||
|
.map(|title_element| title_element.inner_html())
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,34 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
// ANCHOR: all
|
||||||
|
use trpl::{Either, Html};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
|
||||||
|
trpl::run(async {
|
||||||
|
let title_fut_1 = page_title(&args[1]);
|
||||||
|
let title_fut_2 = page_title(&args[2]);
|
||||||
|
|
||||||
|
let (url, maybe_title) =
|
||||||
|
match trpl::race(title_fut_1, title_fut_2).await {
|
||||||
|
Either::Left(left) => left,
|
||||||
|
Either::Right(right) => right,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{url} returned first");
|
||||||
|
match maybe_title {
|
||||||
|
Some(title) => println!("Its page title is: '{title}'"),
|
||||||
|
None => println!("Its title could not be parsed."),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn page_title(url: &str) -> (&str, Option<String>) {
|
||||||
|
let text = trpl::get(url).await.text().await;
|
||||||
|
let title = Html::parse(&text)
|
||||||
|
.select_first("title")
|
||||||
|
.map(|title| title.inner_html());
|
||||||
|
(url, title)
|
||||||
|
}
|
||||||
|
// ANCHOR_END: all
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,21 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
// ANCHOR: all
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
trpl::spawn_task(async {
|
||||||
|
for i in 1..10 {
|
||||||
|
println!("hi number {i} from the first task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for i in 1..5 {
|
||||||
|
println!("hi number {i} from the second task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// ANCHOR_END: all
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,23 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: handle
|
||||||
|
let handle = trpl::spawn_task(async {
|
||||||
|
for i in 1..10 {
|
||||||
|
println!("hi number {i} from the first task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for i in 1..5 {
|
||||||
|
println!("hi number {i} from the second task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle.await.unwrap();
|
||||||
|
// ANCHOR_END: handle
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,25 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: join
|
||||||
|
let fut1 = async {
|
||||||
|
for i in 1..10 {
|
||||||
|
println!("hi number {i} from the first task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let fut2 = async {
|
||||||
|
for i in 1..5 {
|
||||||
|
println!("hi number {i} from the second task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
trpl::join(fut1, fut2).await;
|
||||||
|
// ANCHOR_END: join
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,15 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: channel
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let val = String::from("hi");
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
|
||||||
|
let received = rx.recv().await.unwrap();
|
||||||
|
println!("Got: {received}");
|
||||||
|
// ANCHOR_END: channel
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,27 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: many-messages
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
// ANCHOR_END: many-messages
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,33 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
// ANCHOR: futures
|
||||||
|
let tx_fut = async {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
trpl::join(tx_fut, rx_fut).await;
|
||||||
|
// ANCHOR_END: futures
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,33 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: with-move
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
eprintln!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
trpl::join(tx_fut, rx_fut).await;
|
||||||
|
// ANCHOR_END: with-move
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,48 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
// ANCHOR: here
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx1.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("more"),
|
||||||
|
String::from("messages"),
|
||||||
|
String::from("for"),
|
||||||
|
String::from("you"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_millis(1500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
trpl::join3(tx1_fut, tx_fut, rx_fut).await;
|
||||||
|
// ANCHOR_END: here
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,48 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx1.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("more"),
|
||||||
|
String::from("messages"),
|
||||||
|
String::from("for"),
|
||||||
|
String::from("you"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ANCHOR: here
|
||||||
|
trpl::join!(tx1_fut, tx_fut, rx_fut);
|
||||||
|
// ANCHOR_END: here
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,50 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx1.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("more"),
|
||||||
|
String::from("messages"),
|
||||||
|
String::from("for"),
|
||||||
|
String::from("you"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ANCHOR: here
|
||||||
|
let futures = vec![tx1_fut, rx_fut, tx_fut];
|
||||||
|
|
||||||
|
trpl::join_all(futures).await;
|
||||||
|
// ANCHOR_END: here
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1,51 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx1.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("more"),
|
||||||
|
String::from("messages"),
|
||||||
|
String::from("for"),
|
||||||
|
String::from("you"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ANCHOR: here
|
||||||
|
let futures =
|
||||||
|
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
||||||
|
|
||||||
|
trpl::join_all(futures).await;
|
||||||
|
// ANCHOR_END: here
|
||||||
|
});
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
trpl = { path = "../../../packages/trpl" }
|
@ -0,0 +1 @@
|
|||||||
|
$
|
@ -0,0 +1,51 @@
|
|||||||
|
extern crate trpl; // required for mdbook test
|
||||||
|
|
||||||
|
use std::{future::Future, time::Duration};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("hi"),
|
||||||
|
String::from("from"),
|
||||||
|
String::from("the"),
|
||||||
|
String::from("future"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx1.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rx_fut = async {
|
||||||
|
while let Some(value) = rx.recv().await {
|
||||||
|
println!("received '{value}'");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx_fut = async move {
|
||||||
|
let vals = vec![
|
||||||
|
String::from("more"),
|
||||||
|
String::from("messages"),
|
||||||
|
String::from("for"),
|
||||||
|
String::from("you"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for val in vals {
|
||||||
|
tx.send(val).unwrap();
|
||||||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ANCHOR: here
|
||||||
|
let futures: Vec<Box<dyn Future<Output = ()>>> =
|
||||||
|
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
||||||
|
// ANCHOR_END: here
|
||||||
|
|
||||||
|
trpl::join_all(futures).await;
|
||||||
|
});
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue