Installing Rust on Windows and Visual Studio Code with WSL

Photo by Colton Sturgeon on Unsplash

It was suggested to me to look into the Rust programming language after I expressed interest in learning more about systems concepts. Installing Rust was simple but there were a couple of snags along the way. That’s why I’ve decided to write a short blog about the process I had to go through to get Rust working on VS Code with WSL on Windows 10.

Installing Rust

First Step

Run curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh in the command line. I ran this in the user directory in VS Code, which is where it defaults to when I open a terminal in a new window. When it prompted me I selected option 1 for default installation options.

Second Step

I installed Microsoft C++ Build tools next. You can go here to find them. I had to run it as administrator to get the installer to launch. Make sure C++ build tools are selected and that the Windows 10 SDK and the English language pack components are included. I only had to check the C++ build tools box and the Windows 10 SDK and English language pack came as a part of it.

At this point I had to close VS Code before running rustc--version in the command line to see that Rust was properly installed.

Cargo

After following those steps to get Rust installed, I began reading through Rust’s documentation and following along with the simple coding exercises at the beginning. Cargo is Rust’s build system and package manager, and after following those steps to get Rust installed it was already on my system.

In order to create a new project directory with Cargo you will run cargo new project_name. It will generate a simple file directory with a src folder a .gitignore file and a Cargo.toml file. Cargo.toml is Cargo’s manifest, TOML stands for Tom’s Obvious, Minimal Language. Cargo uses this file to compile your code, including dependencies, and ensures that you will always get a repeatable build. To start your Cargo.toml file will look something like this:

[package]name = "project_name"version = "0.1.0"authors = ["Your Name <your email>"]edition = "2018"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]

Cargo Causes an Error

To start with Cargo will generate a simple hello world program. You can run cargo build which creates an executable file in target\debug\project_name.exe . You can manually run that file with .\target\debug\project_name.exe, alternatively you can run cargo run to both build and run the file. When I ran either of these I got a warning that read error: Permission denied (os error 13). As far as I can tell, my code compiled successfully and it generated a Cargo.lock file.

If you’re familiar with JavaScript, Cargo.lock is similar to to a package-lock.json file. The Cargo.toml file is what you write, in part to describe which dependencies to use. Cargo.toml uses Semantic Versioning which means you aren’t asking for a specific version, rather any version that has a public API compatible with the specified version. Whereas your Cargo.lock file could be thought of as the specific directions and exact versions of the dependencies required to compile your code, and you do not alter that file.

Even though it seemed everything was working, I did some digging to try and figure out why I was getting an error. That’s when I stumbled upon this closed issue on GitHub.

Photo by Markus Winkler on Unsplash

VS Code is the Culprit

It seems VS Code is responsible for the error. One user commented:

They employ a file watcher that listens to file events to be notified about changes to files in an opened folder. Somehow that locks the files and causes permission errors.

However the link they provided to the solution no longer seemed to go to the same page and I had to do a little more digging. I came across another issue on GitHub, for an unrelated error, but it provided me with the solution to my bug as well.

{
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/target/**": true
}
}

The last line is what I needed to change in my VS Code settings.

So I opened up my settings with Ctrl + , then I searched for Watcher Exclude, this is what should pop up:

Watcher Exclude on VS Code

You’ll click the Add Pattern button and enter **/target/** then hit OK.

After that, it took me creating a new project with Cargo to get the error to go away.

Conclusion

Installing Rust wasn’t too difficult, and their documentation is top notch. The only thing I’m not sure about is whether or not I needed to install the C++ Build Tools as well. It seems most people using WSL and VS Code install an extension instead. I hope that you found this blog informative, and if you were getting the same error that it cleared it up for you!

Aspiring Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store