Code Rust in Windows

I have started to learn rust, and I am enjoying myself. This is a merge and update on the two fantastic blog posts on how to setup Visual Studio Code for Rust and how to enable debugging. In my personal opinion, from among all the available IDE solutions for rust, this is the best.

Setup Rust

First, install rust. Open a command line window and do this:

rustup default stable-x86_64-pc-windows-gnu
cargo install racer
cargo install rustfmt
cargo install rustsym
rustup component add rust-src
rustup component add rust-docs

after that, we need to set an important environment variable. Put the following in a batch file and run it, or understand it and do whatever the hell you want to make that environment variable stick.

@ECHO OFF
FOR /F "tokens=*" %%P IN ('rustc --print sysroot') DO SET RSP=%%P
SETX RUST_SRC_PATH "%RSP%\lib\rustlib\src\rust\src"

Create a Test Project

For the sake of setting everything up, let us create a test project P:\rust\test, which we set up as follows:

C:\>P:
P:\>cd rust
P:\rust>cargo new test --bin
     Created binary (application) `test` project

P:\rust>cd test
P:\rust\test>dir
 Volume in drive P is awesome
 Volume Serial Number is DEAD-BEEF

 Directory of P:\rust\test

2059-02-22  13:37    <DIR>          .
2059-02-22  13:37    <DIR>          ..
2059-02-22  13:37                 7 .gitignore
2059-02-22  13:37               100 Cargo.toml
2059-02-22  13:37    <DIR>          src
               2 File(s)            107 bytes
               3 Dir(s)              12 bytes free

This example should be sufficiently general so that you can deduce your particular requirements from the rest of this tutorial.

Setup Visual Studio Code

  • Get Visual Studio Code and
  • install Rusty Code, Native Debug, and the Better TOML Support plugins.
    Hint: Press Ctrl+Shift+X in VSC.
  • Open the P:\rust\test directory in Visual Studio Code.
  • If you are using git from cygwin, then you will get a bunch of annoying errors. This SO answer gives a hint on how to solve it:
    mkdir c:\cygdrive
    mklink /j c:\cygdrive\c c:
    mklink /j c:\cygdrive\p p:
  • Go to File / Preferences / Settings and enter the following. Or, obviously, modify it according to your needs. Also see Rusty Code's Github page for Rusty Code specific settings.
    {
      "editor.fontFamily": "Source Code Pro, Consolas, 'Courier New', monospace",
      "editor.fontSize": 16,
      "rust.formatOnSave": true
    }
  • Go to File / Preferences / Keyboard Shortcuts and enter the following. Again, just a recommendation.
    [
        { "key": "ctrl+alt+r", "command": "workbench.action.tasks.runTask" },
        { "key": "f7", "command": "workbench.action.tasks.build" }
    ]
  • Make yourself familiar with the other shortcuts.

Setup Tasks in VSC

Press Command+Shift+P and select Tasks: Configure Task Runner. Enter the following. Refer to the VSC task documentation for what is going on here.

{
 "version": "0.1.0",
 "command": "cargo",
 "isShellCommand": true,
 "tasks": [
  {
   "taskName": "doc",
   "command": "rustup",
   "showOutput": "never",
   "args": ["doc"]
  },
  {
   "taskName": "build",
   "isBuildCommand": true,
   "showOutput": "always",
   "problemMatcher": {
    "owner": "rust",
    "fileLocation": [
     "relative",
     "${workspaceRoot}"
    ],
    "pattern": {
     "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
     "file": 1,
     "line": 2,
     "column": 3,
     "endLine": 4,
     "endColumn": 5,
     "severity": 6,
     "message": 7
    }
   }
  },
  {
   "taskName": "clean",
   "showOutput": "always"
  },
  {
   "taskName": "run",
   "showOutput": "always"
  },
  {
   "taskName": "test",
   "showOutput": "always",
   "isTestCommand": true,
   "problemMatcher": [
    {
     "owner": "rust",
     "fileLocation": [
      "relative",
      "${workspaceRoot}"
     ],
     "pattern": {
      "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
      "file": 1,
      "line": 2,
      "column": 3,
      "endLine": 4,
      "endColumn": 5,
      "severity": 6,
      "message": 7
     }
    },
    {
     "owner": "rust",
     "fileLocation": [
      "relative",
      "${workspaceRoot}"
     ],
     "severity": "error",
     "pattern": {
      "regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
      "message": 1,
      "file": 2,
      "line": 3
     }
    }
   ]
  }
 ]
}

Build and Run Your Project

Hit F7 to build your project and Ctrl+Shift+R to run it. That should already work. You can also hit Ctrl+Alt+R to open the task runner menu, then enter doc (so it reads task doc) and your browser will open with the locally installed copy of the rust documentation.

Setup Debugging in VSC for Rust

  • Press Ctrl+Shift+D to open the debugging pane.
  • Create a new configuration, pick any template and replace it with this:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug",
                "type": "gdb",
                "request": "launch",
                "target": "target/debug/${workspaceRootFolderName}",
                "cwd": "${workspaceRoot}"
            }
        ]
    }
  • Insert a breakpoint somewhere.
  • Win.

Full Disclosure

I haven't yet created a second rust project. I am solemnly working exercises in my safe space at P:\rust\test. I am not sure how much of this has to do be repeated for the next project. When the time comes, this blog post will be updated.

Leave a Reply

Your email address will not be published. Required fields are marked *