error[E0282]: type annotations needed --> src/main.rs:14:9 | 11 | let fut = async { | --- consider giving `fut` a type ... 14 | Ok(1) | ^^ cannot infer type for type parameter `E` declared on the enum `Result`
原因在于编译器无法推断出 Result<T, E> 中的
E 的类型, 而且编译器的提示
consider givingfuta type
你也别傻乎乎的相信,然后尝试半天,最后无奈放弃:目前还没有办法为
async 语句块指定返回类型。
error: future cannot be sent between threads safely --> src/main.rs:17:18 | 17 | require_send(foo()); | ^^^^^ future returned by `foo` is not `Send` | = help: within `impl futures::Future<Output = ()>`, the trait `std::marker::Send` is not implemented for `Rc<()>` note: future is not `Send` as this value is used across an await --> src/main.rs:11:5 | 10 | let x = NotSend::default(); | - has type `NotSend` which is not `Send` 11 | bar().await; | ^^^^^^^^^^^ await occurs here, with `x` maybe used later 12 | } | - `x` is later dropped here
提示很清晰,.await在运行时处于 x
的作用域内。在之前章节有提到过, .await
有可能被执行器调度到另一个线程上运行,而 Rc 并没有实现
Send,因此编译器无情拒绝了咱们。
error[E0706]: functions in traits cannot be declared `async` --> src/main.rs:5:5 | 5 | async fn test(); | -----^^^^^^^^^^^ | | | `async` because of this | = note: `async` trait functions are not currently supported = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait