Skip to content

[E0670]: async fn is not permitted in Rust 2015 (but I'm using Rust 2022) #12233

Closed
@amab8901

Description

@amab8901

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: 5d5bbec 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-supportCategory: support questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions