如何使用 Rust 2018 或更高版本来实现`async fn`?

问题描述 投票:0回答:1

我正在尝试用 Rust 编译一个库,但出现错误:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> src/lib.rs:1:1
  |
1 | async fn foo() {
  | ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`

这是我的 Cargo.toml:

[package]
name = "name"
version = "0.1.0"
edition = "2018"

为了编译,我使用以下命令行(文档here):

rustc --crate-type=lib src/apis/my_file.rs

有人有想法吗?

rust async-await rust-2018
1个回答
5
投票

顾名思义,

Cargo.toml
cargo
解释,而不是
rustc
,如果你想用
rustc
编译,你必须添加版本标志:

rustc --edition=2018 --crate-type=lib src/apis/my_file.rs
© www.soinside.com 2019 - 2024. All rights reserved.