简体   繁体   English

Rust 单元测试中的错误:“函数声明中缺少 async 关键字”

[英]Error in Rust unit test: "The async keyword is missing from the function declaration"

I'm working on a Rust workspace which uses tokio 0.2.6 and futures 0.3.0 and I get the following error trying to compile this code:我正在使用 tokio 0.2.6 和 futures 0.3.0 的 Rust 工作区工作,尝试编译此代码时出现以下错误:

#[cfg(test)]
mod tests {

    #[test]
    fn test() {
        assert_eq!(true, true)
    }
}
error: the async keyword is missing from the function declaration
   --> api/src/order.rs:299:5
    |
299 |     fn test() {
    |     ^^

This error makes no sense to me, because it is just a plain-old unit test that doesn't do anything async.这个错误对我来说毫无意义,因为它只是一个不做任何异步操作的普通单元测试。 More over, the entire project this code is located in doesn't do async either.此外,此代码所在的整个项目也不执行异步操作。 There are other projects in the workspace which are heavy on async code using both tokio and futures, but I'm not seeing how these affect each other.工作区中还有其他项目使用 tokio 和 futures 都在使用异步代码,但我没有看到它们是如何相互影响的。

As user mcarton noted, the problem occurred because I had use tokio::*;正如用户 mcarton 所指出的,出现问题是因为我use tokio::*; in my project somewhere.在我的项目中的某个地方。 Removing that import solved the problem.删除该导入解决了问题。

For me it was:对我来说是:

#[macro_use]
extern crate tokio;

It only worked after I removed it.它只有在我删除它后才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM