-
Notifications
You must be signed in to change notification settings - Fork 1
/
async_in_rust_31_async_await.rs
61 lines (50 loc) · 1.92 KB
/
async_in_rust_31_async_await.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(dead_code)]
#![allow(unused_variables)]
#![feature(future_join)]
use std::future::Future;
// async/await 的使用
// `foo()` 返回一个实现了 `Future<Output = u8>` 的类型。
// `foo().await` 将会产生一个 u8 类型的值。
async fn foo() -> u8 { 5 }
fn bar() -> impl Future<Output = u8> {
// 这个 `async` 块会产生一个实现了 `Future<Output = u8>` 的类型。
async {
let x: u8 = foo().await;
x + 5
}
}
// async lifetime
// 与普通的函数不一样,async fn 会获取引用或其他非静态生命周期的参数,然后返回被这些参数的生命周期约束的 Future:
async fn foo_lifetime(x: &u8) -> u8 { *x }
// 这与上面的函数完全等价
fn foo_expanded<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
async move { *x }
}
// 把带有引用参数的 async fn 转化为静态 Future 的解决方法是:把参数和对 async fn 的调用封装到 async 块中
async fn borrow_x(x: &u8) -> u8 { *x }
fn bad() -> impl Future<Output = u8> {
let x = 5;
// borrow_x(&x) // ERROR: `x` does not live long enough
// borrowed value does not live long enough
// argument requires that `x` is borrowed for `'static`
async move { x }
}
fn good() -> impl Future<Output = u8> {
async {
let x = 5;
borrow_x(&x).await
}
}
// async move
// this part need executor, so need run with cargo and import futures crate
// 不同的 async 块可以访问相同的变量s,只要它们都在s的作用域范围内执行
async fn blocks() {
let s = String::from("Hello World!");
let future_one = async { println!("{:?}", s); };
let future_two = async { println!("{:?}", s); };
// futures::future::join(future_one, future_two); // need run in cargo with futures crate
std::future::join!(future_one, future_two); // macro join!() can join multiple futures
// https://doc.rust-lang.org/std/future/macro.join.html
}
fn main() {
}