Rust Visual Studio Code



Installing Rust and VSCode The best way to install Rust is via Rustup You can grab Visual Studio Code from here You may also need to run rustup component add rust-src if you wish to step into standard library components (mentioned below). Open Visual Studio Code and go to./src/main.rs and place a breakpoint. Edit launch.json. Press F5 to run the project. A selection box will appear that will ask you what environment you want to use. Choose LLDB Debugger here. This will open up the launch.json file, and here we need to tell Visual Studio Code how to launch our project. Edit the file so it looks similar to this. Visual Studio Code(下記ではVSCodeと呼びます)をインストールした後、VSCodeを実行します。 左側のメニューから「Extensions」を見つけ、rlsとNative Debugの2つの拡張機能を見つけてインストール. I want to build a rust-project from visual studio 2019 (windows). Here is what I have done so far: Downloaded and installed rust. Compiling via rustc from the command line is possible. Installed the Rust Add-On in Visual Studio. Set 'Toolchain to use' from 'nightly' to 'beta'. This solved an issue where I got the message that rls could not be. Rust support for Visual Studio Code. Adds language support for Rust to Visual Studio Code. Supports: code completion; jump to definition, peek definition, find all references, symbol search; types and documentation on hover; code formatting; refactoring (rename, deglob) error squiggles and apply suggestions from errors; snippets; build tasks.

I thought I’d share my experiences of using Rust in Visual Studio Code, you can find the original post on my blog: https://booyaa.wtf/2017/rust-vscode/index.html

Which extension?

Visual

First off if you can, you should be using the Rust Language Server (RLS) extension. Yes it’s beta, but the user experience has been the best I’ve had, outside of in-house language support by giants like Microsoft and JetBrains! The extension will even install dependencies for you if you have rustup installed!

If you didn’t want to use RLS, then the alternative is to install various Rust related tools (racers, rustfmt and rustsym) manually. The only Rust extension that support non-RLS is Kalita Alexey’s.

Whilst we’re on the subject of Kalita’s extension, this was a fork of the RustyCode extension which was no longer being actively maintained.

Rust

Rust Visual Studio Code

Debug

The biggest draw this extension at the time was that it was available on the Visual Studio Marketplace, where as the RLS team extension had to be manually installed via git.

I think one of reasons why Kalita released his fork into the Visual Studio Marketplace, was that the first Rust extension

It will be interesting two see how the two active extensions progress.

If you want a proper whistle stop tour of RLS I recommend you pop over to @nrc blog as he’s done a thorough job of it in this post.

Rust visual studio code linux

Debugging

I’m ashamed to admit that I still find setting up debugging Rust a bit of a black art if you’re not using gdb. However thanks to the LLDB Debugger extension it’s become a little bit easier.

Rust Visual Studio Codes

The only bit that caught me out was the launch.json boiler plate code (see below for a sample), specifically what would be the correct value for program key. This is the path to your debug binary.

So if the binary you wish to debug is called foo, your value for the program key would look like this:

note: I’ve omitted the rest of the json keys that don’t change for the sake of brevity.

If you wanted to keep things generic and only compile a binary that matches the cargo folder name, you could use ${workspaceRootFolderName} variable substitution.

Visual

If you’re interested in what other variables substitutions are available the Visual Studio Code Debugger guide has a handy list.

One last option to enable is sourceLanguages with the value of 'rust', this option enables visualisation of built-in types and standard library types i.e. you can peek into the contents of a Vec etc.

Here’s a complete example of the launch.json for reference.

What’s your favourite parts of the new Rust extension? Did I miss anything notable with the LLDB extension? Please share!





Comments are closed.