Full Rust setup in VSCode/Atom Issue

3k Views Asked by At

This is a long post, sorry.

I have been trying to start a project, using Rust, but ran into a problem: it just does not work correctly on Linux in VSCode/Atom.

Wasted 3 days, searching online, trying different tutorials/videos - nothing worked + most of the material is from 2017. I have tried Matrix chat, but no one knew what to do. Git too has no solution as people keep suggesting very random things, like "change this variable in toml file to something else, and back again"

Git: https://github.com/rust-lang/rls-vscode/issues/513

I installed (and re-installed rust many times in the last 3 days), and it works just fine from the terminal, but not in the Editor.


Two Issues:

  1. Editors don't see any crates, so you can't run your code from the editor.
  2. Autocomplete does not work (only works on std, not on extra crates you add).

What I did (out of many other things):

  1. install Rust (on Manjaro and Debian computers): curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Tried stable, beta, nightly (for racer) Just did a clean install again:

stable installed - rustc 1.40.0 (73528e339 2019-12-16)

Rust is installed now. Great!

  1. Installed Rust (rls) rust-lang.rust extension in VSCode (and rust in Atom)

  2. Created a new project: cargo new test_proj and added new rand = "0.6" to [dependencies] and used cargo build. It did build rand

  3. Added "rust-client.enableMultiProjectSetup": true to the settings file to avoid a warning: A Cargo.toml file must be at the root of the workspace in order to support all features. Alternatively set rust-client.enableMultiProjectSetup=true in settings. by Rust (rls)

  4. I also install 'code runner' extension that I use with Python, C++, and Java, to run the code from within the editor.


So now I have just the main func and it runs just fine from the editor:

enter image description here

enter image description here


Now I add rand and it seems to work, and does SOME auto-completion...

enter image description here

enter image description here


But now it stops working:

enter image description here


OK, I will finish the code and try running it, And now we can't run it anymore as the crate is missing:

enter image description here


OK, let's try Ctrl + Shift + B and try cargo build:

enter image description here

enter image description here

For some odd reason, it is looking in /media/Work/Work/rust_code and not in /media/Work/Work/rust_code/test_proj/


One last thing: let's try running in the terminal:

enter image description here

So it does work just fine.

Sorry for the long post, but I have wasted 3 days now and it still can't get it up and running. Did anyone manage to set this up at all?

It has to be Atom or VSCode as I have all of the other languages/projects setup there + VSCode is listed on the official Rust website, so I presume it should work.

Basically, out of two editors (VSCode and Atom), that have Debugging capabilities, unlike Intellij Rust, both don't work for me and I just can't code in Rust as tools are literally broken/not mature enough for productive work. Please let me know if I am wrong and it is just a case of one little flag, that everyone forgets to mention, is missing in some config.

2

There are 2 best solutions below

0
On BEST ANSWER

I have figured out one part: problems with running your code from within the VSCode. I had to modify default code-runner command for rust:

Original command:

"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",

Changed to:

"rust": "if [ $(basename $dir) = 'examples' ]; then cargo run --example $fileNameWithoutExt; else cargo run; fi",

And now it works, and I can run my code quickly.

Part 2: Autocomplete still is very very bad, unfortunately. I am using RLS. there is this thing racer. Is it a better choice?

Alternatively, I have heard praise about using IntelliJ + the Rust plugin, but I haven't used it myself yet.

As I mentioned in my previous post, IntelliJ, free edition does not have a debugger from what I can see. I need it.

I am wondering how people get a good autocomplete from Rust?

0
On

Short Version

Assuming I understand the issue correctly. Then the various build issues boil down to different ways you're trying to build the project, and attempting to do so in different directories.

TL;DR: The directory you have open in VSCode is the rust_code directory. Close it and instead open rust_code/test_proj. Now the Rust: cargo build (or Rust: cargo run) should work.


Long Version

Editors don't see any crates, so you can't run your code from the editor.

The Rust: cargo build task isn't working, because the directory you have open isn't a "Rust project" (Cargo package), it's a directory containing another directory, which is a Rust project.

Looking at your screenshot shows this, look at how the top line says rust_code/test_proj:

When you execute the task, Cargo is complaining that rust_code/Cargo.toml doesn't exist, which is true since it's located in rust_code/test_proj/Cargo.toml

If you look at the output of this screenshot, you can see that is the case:


Here you're executing cargo run manually. But the important difference is that you're inside the rust_code/test_proj directory.


Lastly simply executing rustc main.rs is failing since you aren't passing the arguments needed. So rustc doesn't know anything about your dependencies.

Try executing cargo build -v then you can see all the arguments that Cargo is passing to rustc.


Autocomplete does not work (only works on std, not on extra crates you add).

Try and open a directory that contains a Cargo.toml, then code completion should work for dependencies. If I don't, then I get the following notification, and code completion only works for the standard library as you said. That being said, RLS is weird sometimes.

If I had to guess, then I think RLS compiles the code, and at some stage extracts the needed information. Thus if the code doesn't compile, then code completion could be affected. But this is 100% an educated guess.

Alternatively, I have heard praise about using IntelliJ + the Rust plugin, but I haven't used it myself yet.