There are many fantastic resources for learning Rust, but surprisingly there aren't many resources for setting up Rust in VS Code. Here's how I set up my IDE when I first started:
-
Install Rust (Use this link to download the right version for your operating system. You may also have to download Xcode. On macOS, you can get this C compiler by typing: $ xcode-select --install into the terminal. If you're using a pc, you may have to download C++ build tools.
-
Download Visual Studio
-
Download The Right Extensions There are a few Rust extensions for VS Code. Surprisingly, you shouldn't download the Rust extension. You should instead download the Rust-analyzer. You can do so by searching for it in the extensions tab. Next, install Code Runner by searching in the extensions tab. It can also be helpful to download Better TOML (this helps you read TOML files). A helpful extension for debugging purposes is Code LLDB or the C/C++ (Windows) extension. To make sure all your crates are up to date, you can download the Crates extension. This displays the latest version of the crate next to it in the TOML file, as well as a few other nice features.
-
Change Your Settings (Optional) Whilst not necessary, I think it's important to mention. This step can be the most confusing and frustrating because you may want to turn off something that you don't even know the name of. There are a few, in my opinion, annoying features that get in the way. This is something called inlay hints. They are the white highlighted text. They are not part of your code, but rather indicate what type of object you should consider using. They can be really helpful in certain situations, so I'd make them off until pressed. You can do this by going to settings and searching Editor > Inlay Hints. If you do this, you can press control alt to see the inlay hints whenever you want them! Another feature that may confuse you is auto imports. You start coding your first Rust program and you go to run it. But there's an error. You are importing a function that does not exist. What's strange is that you know that you didn't write that import statement. What happened? Well, at some point you might have called a function that you don't have access to. This prompted the auto import feature to include a function that doesn't exist. The issue is that after you fix your mistake the import statement stays around and causes an error. Some coders (especially those coding in Java Script) find this feature helpful. If you want to turn it off, go to settings and search Rust-analyzer › Completion › Autoimport and disable it. You can also go into settings and search Files: Auto Save and change it to afterDelay if you don't want to manually save after edits. Finally, by default Rust-analyzer uses cargo check to check your source code on save for errors. You can change this to clippy (this checks for all the cargo check errors as well as a bunch of common mistakes / broken Rust style conventions). To do this go to extensions, rust-analyzer, extension settings, search check on save and change check to clippy. Because clippy checks for more errors than cargo check it may take more time to process.
-
Change Your Settings (Mandatory) Finally, the most important and strangely hidden setting that you must change to have VS Code running properly is changing how Code Runner actually runs your code. Many people have tried coding the Guessing Game activity from the Rust book. Their code resulted in errors even when they copy and pasted the solution (1) (2). There have been many "answers" as to why this is happening. None of them are correct. You need to go to extensions, Code Runner, press the gear, Extension Settings, Code-runner: Executor Map (edit in settings.json), and finally change "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt" to "rust": "cargo run # $fileName". This makes Code Runner load the crates you need before you run the code, and will fix the error.
Is there anything I missed? Are there any other suggestions for beginners on how to set up VS Code for Rust? Please let me know in the comments and I'll add them to this post!