rust-analyzer: [E0670]: `async fn` is not permitted in Rust 2015 (but I'm using Rust 2022)

Here’s my code:

async fn main() {}

Here’s the output:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0752]: `main` function is not allowed to be `async`
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0670, E0752.
For more information about an error, try `rustc --explain E0670`.

Here is my Cargo.toml file:

[package]
name = "visualizer"
version = "0.1.0"
edition = '2021'

Here is what it auto-generates in Cargo.lock file:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "visualizer"
version = "0.1.0"

ATTEMPTED SOLUTION # 1:

I removed the async keyword in async fn main() {} so it became fn main() {} in main.rs. Then I did the following in the terminal:

$ cargo fix --edition
    Checking visualizer v0.1.0 ( // I manually removed the path for privacy reasons )
warning: `src/main.rs` is already on the latest edition (2021), unable to migrate further

If you are trying to migrate from the previous edition (2018), the
process requires following these steps:

1. Start with `edition = "2018"` in `Cargo.toml`
2. Run `cargo fix --edition`
3. Modify `Cargo.toml` to set `edition = "2021"`
4. Run `cargo build` or `cargo test` to verify the fixes worked

More details may be found at
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html

    Finished dev [unoptimized + debuginfo] target(s) in 0.29s

So I changed edition = "2021" into edition = "2015" in Cargo.toml. Then I did the following in the terminal:

$ cargo fix --edition
    Checking visualizer v0.1.0 ( // I manually removed the path for privacy reasons  )
   Migrating src/main.rs from 2015 edition to 2018
    Finished dev [unoptimized + debuginfo] target(s) in 0.95s

Then I edited back from edition = "2015" to edition = "2021" and did the following in the terminal:

$ cargo build
   Compiling visualizer v0.1.0 ( // I manually removed the path for privacy reasons )
    Finished dev [unoptimized + debuginfo] target(s) in 0.37s
$ cargo test
   Compiling visualizer v0.1.0 ( // I manually removed the path for privacy reasons  )
    Finished test [unoptimized + debuginfo] target(s) in 0.26s
     Running unittests ( // I manually removed the path for privacy reasons  )

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Then I changed back from fn main() {} to async fn main() {} in main.rs, and here’s the output I get:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0752]: `main` function is not allowed to be `async`
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0670, E0752.
For more information about an error, try `rustc --explain E0670`.

[Done] exited with code=1 in 0.1 seconds

Thus, my attempted solution has failed.


ATTEMPTED SOLUTION # 2

I changed this…

async fn main() {}

into this…

fn main() {}

async fn hello() {}

I run it, and here’s the output:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:3:1
  |
3 | async fn hello() {}
  | ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: aborting due to previous error

For more information about this error, try `rustc --explain E0670`.

ATTEMPTED SOLUTION # 3 I changed main.rs contents into the following:

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() {}

And here is my new Cargo.toml:

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

[dependencies]
tokio = { version= "1", features = ["full"] }

Here’s the output:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:4:1
  |
4 | async fn main() {}
  | ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0432]: unresolved import `tokio`
 --> main.rs:1:5
  |
1 | use tokio::main;
  |     ^^^^^ maybe a missing crate `tokio`?

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
 --> main.rs:3:3
  |
3 | #[tokio::main]
  |   ^^^^^ use of undeclared crate or module `tokio`

error[E0752]: `main` function is not allowed to be `async`
 --> main.rs:4:1
  |
4 | async fn main() {}
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0432, E0433, E0670, E0752.
For more information about an error, try `rustc --explain E0432`.

Rust Version rust-analyzer 2022-05-09

Editor: VSCode

Operating System: Archlinux

rust-analyzer version: rust-analyzer version: 5d5bbec9b 2022-05-09 stable

rustc version: rustc 1.59.0 (9d1b2106e 2022-02-23)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 39 (21 by maintainers)

Most upvoted comments

thank you for the advice, everyone 😃

Yeah, I opened the directory.

And by the way, I’d uninstall the other Rust extensions. Even Bracket Pair Colorizer is built into Code these days.

Almost. You compile a project with cargo build (or cargo run), which in turn runs rustc on your code and on the libraries you’re using. Notice how in that command line there’s:

  • no edition argument, so it defaults to the 2015 edition
  • no reference to tokio, so it complains it doesn’t know what tokio is

cargo run takes care of both. What Code Runner does only works for single-file, no-dependencies programs, and not even then.

In the meanwhile, you can use the this link instead, which does the right thing:

image

https://doc.rust-lang.org/stable/book/title-page.html has a good introduction to cargo.

No, because that’s not the right way to compile a Rust project.

What do you mean by “running main.rs”? In Rust you run a crate, not a file.

The log above shows that your project compiles and runs fine. How are you getting the errors at the bottom?

Oh! After all these things you said about cargo run and running and files and crates, etc, it triggered me to do some research into whether I’m actually supposed to click that “run code” logo on the top right in VSCode. Now I learned that running Rust on VSCode is done quite differently than in other languages such as Python and JavaScript. In Python/JavaScript/etc, you can just click that button at the top right of the VSCode editor. But in Rust, this is wrong. If you want to run the script in Rust, you have to do it via terminal commands, namely cargo build and cargo run. When I put those commands in the terminal (when the work directory is the same as the project’s root folder), then it actually executes the code and without complaining. Thank you everyone 😄

What do you mean by “running main.rs”? In Rust you run a crate, not a file.

The log above shows that your project compiles and runs fine. How are you getting the errors at the bottom?