Problem with Rust-Analyzer

Hey awesome people, I am very new to both programming and linux. I am using Ubuntu 22.0.4 on WSL2 to learn programming using VS Code connected via WSL remote. I created a rust project using "cargo new basic" inside q:/learn/rust. So, now I am working in "q:/learn/rust/basic" and of obviously there's a "src" directory inside the "basic" and "" is inside the "src." All the other files are in the correct directory, cargo.toml, cargo.lock, target directory, everything is in place. "cargo build" builds the file, same with run and check, everything works, except I have three problems.

  1. Gitignore is created but Git is not.(not much important, guess I need to install Git for that.)
  2. Somewhere in the output, I saw something in the terminal saying something like "main function is unused", but I guess that's the full hello world program and I think there's anything more to be done in it.(A bit important, as I need to know, even if we used the function to print hello world, it still says unused.
  3. Now this is the most important and I searched about this problem for almost 3 days and found nothing that solves my problem. The extension, rust-analyzer gives the following error:

[ERROR rust_analyzer::main_loop] FetchWorkspaceError:

rust-analyzer failed to discover workspace

[ERROR rust_analyzer::config] failed to load linked project: project root must point to Cargo.toml or rust-project.json: /mnt/q/Learn/Rust/basic/cargo.toml.

I'd be so grateful if Rustaceans would help me. I too hope to become a Rustacean someday, asap.

Thank you

This usually means you need to open the /mnt/q/Learn/Rust/basic/ folder up rather than something like /mnt/q/Learn/Rust/ or /mnt/q/. The way Rust Analyzer works is to look for a Cargo.toml file at the top level of whatever folder VS Code was opened in.


1 Like

Oh man, you didn't get the problem at all, that's just according to LINUX filesystem, it's inside "Q:/LEARN/RUST/BASIC" as per WINDOWS filesystem.

What I'm saying is to open the Q:/LEARN/RUST/BASIC folder directly, not Q:\.

From your screenshot, I can see that the Cargo.toml is nested down 3 levels and isn't at the top level like in my screenshot.

1 Like
  1. "basic" is the project folder generated with cargo new command so obviously it's inside the "basic" folder. I guess that makes it on the project folder, to compare with your project, your "WAI" folder is my "basic" folder. When I created the new project I was already inside "Rust" folder.

  2. I am new to this and I don't know how to get into the file without the path, even if I drag and drop, the file path are gonna be there.

File > Open > Select Q:\Learn\Rust\basic

1 Like

Best solution, I uninstalled that shitty extension. I think I can do without it. I was even hallucinating the folders I already deleted.
By the way, where can I find "rust_project.json" file? Especially on WSL 2?

I don't know how to close this topic, but thank you all. I was finally able to solve it. Mostly because of your help and then with hours of fussing around on settings, uninstalling and reinstalling the VS Code, I finally figured it out. Here's my solution to anyone facing the same problem in the future.

  1. It was just a learning project, so I deleted the older project, exited any open directories in VS Code, deleted all my previous project folder(this is not recommended for real projects).
  2. I opened the directory where I wanted to create my project and created a new project using cargo command.
  3. Then I opened the project directory from VS Code with file menu.
  4. On settings, I made a reset to previous setting for rust-analyzer extension, and then I restarted the editor and it finally worked.

Thank you, rustaceans. You guys are awesome.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.