[英]Async loop on a new thread in rust: the trait `std::future::Future` is not implemented for `()`
[英]Can a Rust async function work without importing the Future trait?
在下面的代碼中,我沒有導入Future
特征:
async fn hello_world() {
println!("hello, world!");
}
fn main() {
let future = hello_world();
}
但它仍然可以編譯。 但是它不會打印任何東西。
impl MyDatabase { async fn get_user(&self) -> User {... } }
這將被脫糖為類似於:
impl MyDatabase { fn get_user(&self) -> impl Future<Output = User> + '_ {... } }
如果沒有導入Future
特征,如何進行脫糖? 更重要的是,為什么Future
trait 沒有在 Rust 本身中定義,而是在一個 crate 中?
如果沒有導入
Future
特征,如何進行脫糖?
編譯器去糖到完全限定的路徑: core::future::Future
。
為什么
Future
特征沒有在 Rust 本身中定義,而是在一個板條箱中?
它在標准庫中: std::future::Future
。 Rust API文檔有搜索function; 我鼓勵你熟悉它。
Future
特征在添加到標准庫之前在期貨箱中定義,很像Stream
特征。 這兩者現在都存在於標准庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.