Setting up a Rust development environment on Windows 10

Recently took another stab at getting a good Rust development environment going on Windows, and this time it went great!

The ecosystem is maturing fast. Compared to last year, multirust has an excellent successor, rustup, which works pretty much everywhere.

Also the language server project is shaping up well, and has enough functionality to cover most IDE functions.

This is what you'll get by following these instructions:

Install Rust for Windows

So let's get started, from a clean Rust install, to debugging on Windows 10.

  • First, install rustup. Your best option on Windows is the rust-init.exe installer. Just go to the rustup website to find it.

  • With rustup, install at least one Rust toolchain. You can choose between nightly or stable, and building against the Windows or GNU libc. I use nightly (because many libraries require it).

So:

$ rustup install nightly-msvc

or if you want to be more precise, it'll look something like this:

$ rustup install nightly-x86_64-pc-windows-msvc

You specific target and toolchain may vary depending on your machine.

  • Then you you set the default toolchain:
$ rustup default nightly-msvc

You can have both gnu and msvc version around and switch them with rustup. For more on that, look at the rustup docs.

To update your rustup toolchains automatically:

$ rustup self update
$ rustup update nightly //or stable

Install dev tools

There are many tools that makes development with Rust easier, you can install them with cargo:

  • racer: "A utility intended to provide Rust code completion for editors and IDEs"
$ cargo install racer
  • rustsym: "A tool to query symbols from rust code for use in IDEs"
$ cargo install rustsym
  • rustfmt: "A tool for formatting Rust code according to style guidelines"
$ cargo install rustfmt
  • ripgrep : "ripgrep combines the usability of The Silver Searcher with the raw speed of grep"
$ cargo install ripgrep

It's up to you which ones you want to install, but the ones that have been most useful for me so far are rustfmt and racer, though racer is getting superseded by the Rust Language Server, which we will setup momentarily.

  • You can also install the docs for rust:
$ rustup component add rust-doc
  • Last but not least, most of the functionality required by the Visual Studio Code plugin is provided by the Rust Language Server, so let's go ahead and install it:
$ rustup self update
$ rustup update nightly
$ rustup component add rls --toolchain nightly
$ rustup component add rust-analysis --toolchain nightly
$ rustup component add rust-src --toolchain nightly

Now we're ready to go on with installing and setting up Visual Studio Code. You might prefer another editor/IDE, but VSCode is being used as the testbed for the RLS functionality, so I'm sticking with it, as it's most likely to work.

VSCode setup

Assuming you don't have VSCode already, here are the steps to install it from scratch:

  • Get the installer from https://code.visualstudio.com/ and run it.

  • Now you need to install the most recent Rust extension for VSCode, which is vscode-rust. You'll see also RustyCode and vsc-rustfmt lying around, but vscode-rust supersedes both of them. You can install it either by using the command Ctrl+P and typing ext install vscode-rust, or by searching for it on the Extensions tab, the button for which is the last on the toolbar on the left. You'll likely need to reload.

  • Now you need to delve into the settings, to get the language server set up with VSCode. Settings are under File -> Preferences -> Settings. They work by using the left pane to overwrite any default settings shown in the right pane.
    This is what you need to add:

"rust.rustLangSrcPath": "<path of your rust src>",
"rust.rls": {
    "executable": "rustup",
    "args": [
        "run",
        "nightly",
        "rls"
    ],
    "env": {
        "RUST_LOG": "rls=debug"
    }
},

Your rust src should be somewhere like C:\Users\<username>\.rust\rust\src.

  • if you have installed rustfmt you may need to add this as well:
"editor.formatOnSave": true,
"rustfmt.bin": "C:/Users/<username>/.cargo/bin/rustfmt.exe"

or wherever your rustfmt executable ended up being.

Now you should be able to compile, run, get autocompletion, and error lists in VSCode, and more.

You can use the included Powershell to run cargo, or you can use the included Cargo tasks: Ctrl+Shift+P and type cargo to get a list.

Debugging setup

You can have debugging as well.

  • First, add to the settings:
"debug.allowBreakpointsEverywhere": true,
  • Then search for the ms-vscode.cpptools extension and install it. The name it shows is "C/C++". You should have your rustup toolchain set up as msvc.

  • You need to add a Debug Configuration, Debug -> Add Configuration... that should give you a choice that includes "C++ (Windows)".

  • Choose it, and that should create a new launch.json file, that you need to customise for your project, and should end up looking like this:

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceRoot}/target/debug/<yourprojectname>.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true
        }
    ]
}
  • Now you should be able to set a breakpoint, and then do Debug -> Start Debugging, or hit F5

Bonus: change editor theme/font

VSCode has also got an extensive list of optional themes, be sure to check them out by doing File -> Preferences -> Color Theme-> Install Additional Color Themes.... The one I use is Harmonic16 Light theme.

I use Anonymous Pro as a font for all my code editing. If you similarly have a favourite font, you can set it up and change the size, by adding these lines your settings:

"editor.fontFamily": "<your font>, Consolas, 'Courier New', monospace",
"editor.fontSize": 16,

This is all, happy coding!