-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[E0670]: async fn
is not permitted in Rust 2015 (but I'm using Rust 2022)
#12233
Comments
That is not an error produced by rust-analyzer. It is produced by rustc. Do you have |
I have edition 2021. I added a copy of my Cargo.toml in the main post |
Are you sure you're pasting the right code? Looking at the errors:
But your line 1 is use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() {} So the error is for a different version of the code. And the |
What do you mean by "output"? Where do you see this output / what are you doing to get it? I thought at first this was a rustfmt error, but you wouldn't get name resolution errors from rustfmt. |
@amab8901 this is not really a rust-analyzer issue - for general help with Rust there are a few channels where you might get a better response:
If you want people to be able to reproduce your issue, then using links to the rust playground is also a good idea: |
I'm running the code from main.rs through VSCode text editor, using rust-analyzer to run the .rs files. My file hierarchy is as follows: |
I tried using Rust playground and it turns out that the following .rs script works fine:
So the problem then isn't the script itself, but rather there seems to be a deeper issue with either .toml configuration or perhaps my VSCode editor. But I'm not sure what it is more specifically or how to solve it. My Cargo.toml file looks like this:
|
Try uploading your project somewhere (even here) after What's |
Thank you for the advice on using rust playground. I'm not really sure what the issue is, so I can't exclude rust-analyzer from being a potential explanation for the issue. I tried writing the following code on the rust playground...
... and it works fine. This tells me that the main.rs script itself isn't the issue. But rather the issue is something else. Maybe it's the Cargo.toml file. Maybe it's the VSCode text editor itself. Or maybe it's the rust-analyzer (which I'm using for my VSCode to run the .rs files). Or maybe it's something else. |
Try running |
|
Here's what happens:
|
You didn't delete |
ok, I deleted the repository and I will try again. Why do I need to delete "target" though? It's auto-generated whenever I save the |
It's very large and takes a long time to upload and download. |
You need to run |
Here's what happens:
Running main.rs now gives me the following error:
|
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? |
I mean that I have the file open in VSCode and I click the "Run Code" button at the top-right, the one with a triangle pointing to the right. |
Can you upload the code again? |
Just to make sure, where is that "Run code" button located exactly? @lnicola We only provide the "Run" code lens, which is not at the top-right, do we? Or am I misremembering? |
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 |
Good point, flodiebold, it's this lens: and you can also let VS generate a launch configuration: I'm not sure what button @amab8901 is talking about. |
"Run code" button is in the VSCode editor (and some other editors too, such as PyCharm). It's literally at the top-right corner of the editor program. Above the code-editing box, above the scroll sidebar, below the buttons for closing/minimizing window. It has a play-button logo. |
I personally don't use use VSCode, so @lnicola correct me if I'm wrong, but I think that button is provided by some other extension, not built-in to VSCode. So that extension might need to be fixed. (It might be https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner, which does advertise Rust support.) I would also note that you should open the folder containing |
Here is the run button |
Maybe https://marketplace.visualstudio.com/items?itemName=JuanJoseCarracedo.vscode-un-button-script? What extensions do you have installed? |
I have the following extensions: |
Yeah, that's the Code Runner extension. It would have been easier if you had included this line in the log:
|
why would it have been easier? Is it because it reveals whether you clicked the run button or ran it through the terminal? |
No, because that's not the right way to compile a Rust project. |
Oh, so in Rust you compile the entire project as a single piece, while Python in contrast can run individual scripts without running the rest of the project? And does this have to do with the dichotomy between compiled vs interpreted languages? |
Almost. You compile a project with
In the meanwhile, you can use the this link instead, which does the right thing: https://doc.rust-lang.org/stable/book/title-page.html has a good introduction to |
Kind of, yeah. I suspect though that the code runner extension might be doing this because you opened the main.rs as a single file. Try opening the project folder (the one containing Cargo.toml) as a workspace. Maybe the extension will do the right thing in that case. (Unless you already did that, I can't really tell from the screenshots.) (Edit: Ah no, from the breadcrumbs, it looks like you opened the folder already.) |
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. |
thank you for the advice, everyone :) |
Here's my code:
Here's the output:
Here is my Cargo.toml file:
Here is what it auto-generates in Cargo.lock file:
ATTEMPTED SOLUTION # 1:
I removed the
async
keyword inasync fn main() {}
so it becamefn main() {}
in main.rs. Then I did the following in the terminal:So I changed
edition = "2021"
intoedition = "2015"
in Cargo.toml. Then I did the following in the terminal:Then I edited back from
edition = "2015"
toedition = "2021"
and did the following in the terminal:Then I changed back from
fn main() {}
toasync fn main() {}
in main.rs, and here's the output I get:Thus, my attempted solution has failed.
ATTEMPTED SOLUTION # 2
I changed this...
into this...
I run it, and here's the output:
ATTEMPTED SOLUTION # 3
I changed main.rs contents into the following:
And here is my new Cargo.toml:
Here's the output:
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
orCARGO_HOME
)The text was updated successfully, but these errors were encountered: