Code Rust in Windows



I have started to [learn](http://rustbyexample.com/) [rust](http://rust-lang.org), 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](https://mobiarch.wordpress.com/2015/06/16/rust-using-visual-studio-code/) and [how to enable debugging](https://sherryummen.in/2016/09/02/debugging-rust-on-windows-using-visual-studio-code/). In my personal opinion, from among [all the available IDE solutions for rust](https://areweideyet.com/), this is the best. ## Setup Rust First, [install rust](http://rustup.rs/). 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](https://code.visualstudio.com/) and * install [Rusty Code](https://marketplace.visualstudio.com/items/saviorisdead.RustyCode), [Native Debug](https://marketplace.visualstudio.com/items?itemName=webfreak.debug), and the [Better TOML Support](https://marketplace.visualstudio.com/items?itemName=bungcip.better-toml) 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](https://cygwin.com), then you will get a bunch of annoying errors. [This SO answer](http://stackoverflow.com/a/37980938) 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 FilePreferencesSettings and enter the following. Or, obviously, modify it according to your needs. Also see [Rusty Code's Github page](https://github.com/saviorisdead/RustyCode) for Rusty Code specific settings.
{
  "editor.fontFamily": "Source Code Pro, Consolas, 'Courier New', monospace",
  "editor.fontSize": 16,
  "rust.formatOnSave": true
}
* Go to FilePreferencesKeyboard 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](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf). ## Setup Tasks in VSC Press Command+Shift+P and select Tasks: Configure Task Runner. Enter the following. Refer to [the VSC task documentation](https://code.visualstudio.com/Docs/editor/tasks) 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](https://www.rust-lang.org/en-US/documentation.html). ## 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 *