Rust Visual Studio Code code completion not working
Asked Answered
P

8

27

I'm trying to learn Rust and installed the Rust extension for VSCode. But I'm not seeing auto-completions for any syntax.

I'd like to call .trim() on String but I get no completion for it. I read that the completion for Rust isn't great, but the Language Server should be able to recommend methods that can be called on a struct?

I also saw a tutorial where the tutor had autocompletion in VSCode but I don't know what extension he was using.

I also tried it in WSL and thought that WSL was the problem, but after also installing Gigabytes of Visual Studio Build Tools it turns out it's also not working natively on Windows.

enter image description here

Pandich answered 9/10, 2020 at 19:46 Comment(2)
May be related to github.com/rust-analyzer/rust-analyzer/pull/6161Deca
@Deca well thanks I didn't use rust-analyzer before, that was the extension I was missing :) thanksPandich
B
3

As mentioned in dustypomerleau/rust-syntax issue 4, since Nov. 2020, the grammar is merged into Rust Analyzer, and RA's grammar overrides others when it starts.

The completion should improve, and the microsoft/vscode issue 64488 "Need a better grammar support for Rust lang" just got closed (Dec. 2020) as a result.

Bandy answered 1/1, 2021 at 0:13 Comment(1)
also, disable the other extension after installing rust analyzer, because both together lead to conflictsCloris
U
9

I just had the same issue despite using the rust-analyzer extension. Somehow, the Cargo.toml of my current project was not linked to rust-analyzer's linked projects.

To link it, the path of the Cargo.toml must appear in {project_root}/.vscode/settings.json as an argument to rust-analyzer.linkedProjects. Here is how this looks like now in my case:

{
    "rust-analyzer.linkedProjects": [
        "./py-kernel-regression/Cargo.toml",
        "./Cargo.toml",
    ]
}
Unadvised answered 7/5, 2023 at 18:34 Comment(0)
V
7

For me solution was to use Rust Analyzer extension https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer instead of https://marketplace.visualstudio.com/items?itemName=rust-lang.rust

Vanquish answered 7/8, 2021 at 19:5 Comment(2)
that seems to be far more accurate in its suggestionsLibrarianship
That doesn't work for me :(Tatterdemalion
B
3

As mentioned in dustypomerleau/rust-syntax issue 4, since Nov. 2020, the grammar is merged into Rust Analyzer, and RA's grammar overrides others when it starts.

The completion should improve, and the microsoft/vscode issue 64488 "Need a better grammar support for Rust lang" just got closed (Dec. 2020) as a result.

Bandy answered 1/1, 2021 at 0:13 Comment(1)
also, disable the other extension after installing rust analyzer, because both together lead to conflictsCloris
K
2

None of the above answers worked for me.

After a lot of trials, below is what worked. I tested it for 2weeks and then wrote it here.

now code completion is working in every file.

[edit1] After a few months of usage if rust code completion is not working then...

  1. close the vs code
  2. go to the dir where the rust code is written
  3. delete cargo.lock .vscode folders
  4. open vs code > new window > and reimport the project.
Kilburn answered 13/3, 2023 at 11:11 Comment(0)
R
2

One more thing, if your file is not added to mod.rs VSCode will fail to recognize as a project file, and hence the autocomplete suggestions may not work.

Or in main.rs your module is not listed at the top:

mod file_model;
mod utils;
// add relevant module or file
Reyreyes answered 1/6, 2023 at 0:26 Comment(1)
mod.rs is not the only place for module declarations, but yes a missing mod myfilename; will cause auto-complete and other assists to not work.Inguinal
S
1

I know it's weird and noticed nobody has ever mentioned it. If you are on macOS and installed rust via homebrew, then did rustup component add rust-analyzer, vs code will NOT generate auto completion.

There's also a simple solution, which requires you to completely uninstall homebrew-installed rust (or rustup), and then install a new rustup from official install guide. After a rustup component add rust-analyzer this time, vs code starts generating auto completions and doing full-powered analysis.

By completely uninstall your previous rust setup, I mean to rustup component remove all components and rm -rf "$HOME/.cargo" and rm -rf "$HOME/.rustup", or those two customized directories, and brew rm homebrew-installed rust.


Environment: macOS 14.1 (23B74) (Intel), vs code rust extension is rust-analyzer

Solution is verified working in above environment.

Seligman answered 12/11, 2023 at 4:21 Comment(0)
S
0

resolved within entering docker via VS Code and adding rust analyzer extention

Sayers answered 3/5, 2024 at 7:58 Comment(0)
A
0

In my case, the problem was that rustup was not installed. I installed Rust from my distribution packages (NixOS). It was OK for a temporary try, but the installation of rustup solved the issue completely.

Arsonist answered 22/6, 2024 at 20:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.