Visual Studio Rust



Rust Extension Pack is a collection of extensions that can help you write and test Rust applications in Visual Studio Code. 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.

At the very beginning, I want to share my development environment for Rust. While I’m writing, I changed my mind in the middle of writing adding debugger’s too. It seems more valuable to help others.

Visual Studio integration for the Rust programming language (http://www.rust-lang.org/). Visual Studio extension for Rust Currently in development, and not feature complete. Stable versions are available on the Visual Studio extension gallery. Unstable, but more recent builds are downloadable from AppVeyor (choose 'Configuration: Release' and 'Artifacts').

I use the Visual Studio Code for creating Rust applications. My environment is not the best. To hear about your development environment, I want to share mine first. Please share your environments in replies. I’m open to the new one. For simplicity and lightweight, I select the Visual Studio Code. It’s powerful too. Then, the most important things are the extensions.

Extensions

Visual Studio Code Rust

crates

The crates extension helps me managing packages in the project. [1]

rust-analyzer

While programming in Rust, it gives me warnings, errors, and hints when calling functions. It boosts productivity a lot. [2]

I try to find about debugging in Rust. How we can use debugger with the Visual Studio Code.

dbg! macro - the Basic

First, the most basic one prints the variable, or we can use println! macro.

The dbg macro[3] gives more information than the println macro. It shows a line number and a file name too.

gdb(pwndbg)

I think the pwndbg [4] makes the GDB [5] more visually friendly. Unfortunately, I have been failed to use the pwndbg since this issue arises [6]. I saw how does it look like while debugging. The package brings the debugged source code to the top part of a screen. Instead of pure GDB, I can debug by watching the source.

Here are postings about how to use pwndbg. https://blog.xpnsec.com/pwndbg/https://www.ins1gn1a.com/basics-of-gdb-and-pwndbg/

CodeLLDB - Step by Step Debugger

I followed these steps [7] to install the CodeLLDB [8]. It’s an extension in the Visual Studio Code. All the steps are working. My versions are followings.

In step 7, I need to set “launch.json” and I used this.

Run

I can use a step-by-step debugger as the following image. On the left sidebar, we can see the variable ‘a’ is 3.

References

-->

In the Overview of developing on Windows with Rust topic, we introduced Rust and talked about what it is and what some of its main moving parts are. In this topic, we'll set up our development environment.

We recommend that you do your Rust development on Windows. However, if you plan to locally compile and test on Linux, then developing with Rust on the Windows Subsystem for Linux (WSL) is also an option.

Install Visual Studio (recommended) or the Microsoft C++ Build Tools

On Windows, Rust requires certain C++ build tools.

You can either download the Microsoft C++ Build Tools, or (recommended) you might prefer just to install Microsoft Visual Studio.

Note

We'll be using Visual Studio Code as our integrated development environment (IDE) for Rust, and not Visual Studio. But you can still install Visual Studio without expense. A Community edition is available—it's free for students, open-source contributors, and individuals.

While installing Visual Studio, there are several Windows workloads that we recommend you select—.NET desktop development, Desktop development with C++, and Universal Windows Platform development. You might not think that you'll need all three, but it's likely enough that some dependency will arise where they're required that we feel it's just simpler to select all three.

New Rust projects default to using Git. So also add the individual component Git for Windows to the mix (use the search box to search for it by name).

Install Rust

Next, install Rust from the Rust website. The website detects that you're running Windows, and it offers you 64- and 32-bit installers of the rustup tool for Windows, as well as instructions on installing Rust to the Windows Subsystem for Linux (WSL).

Tip

Rust works very well on Windows; so there's no need for you to go the WSL route (unless you plan to locally compile and test on Linux). Since you have Windows, we recommend that you just run the rustup installer for 64-bit Windows. You'll then be all set to write apps for Windows using Rust.

When the Rust installer is finished, you'll be ready to program with Rust. You won't have a convenient IDE yet (we'll cover that in the next section—Install Visual Studio Code). And you're not yet set up to call Windows APIs. But you could launch a command prompt (the x64 Native Tools Command Prompt for VS, or any cmd.exe), and perhaps issue the command cargo --version. If you see a version number printed, then that confirms that Rust installed correctly.

If you're curious about the use of the cargo keyword above, Cargo is the name of the tool in the Rust development environment that manages and builds your projects (more properly, packages) and their dependencies.

And if you really do want to dive in to some programming at this point (even without the convenience of an IDE), then you could read the Hello, World! chapter of the The Rust Programming Language book on the Rust website.

Install Visual Studio Code

By using Visual Studio Code (VS Code) as your text editor/integrated development environment (IDE), you can take advantage of language services such as code completion, syntax highlighting, formatting, and debugging.

Visual Studio Rust Support

VS Code also contains a built-in terminal that enables you to issue command-line arguments (to issue commands to Cargo, for example).

  1. First, download and install Visual Studio Code for Windows.

  2. After you've installed VS Code, install the rust-analyzerextension. You can either install the rust-analyzer extension from the Visual Studio Marketplace, or you can open VS Code, and search for rust-analyzer in the extensions menu (Ctrl+Shift+X).

  3. For debugging support, install the CodeLLDB extension. You can either install the CodeLLDB extension from the Visual Studio Marketplace, or you can open VS Code, and search for CodeLLDB in the extensions menu (Ctrl+Shift+X).

    Note

    An alternative to the CodeLLDB extension for debugging support is the Microsoft C/C++ extenson. The C/C++ extension doesn't integrate as well with the IDE as CodeLLDB does. But the C/C++ extension provides superior debugging information. So you might want to have that standing by in case you need it.

    You can either install the C/C++ extension from the Visual Studio Marketplace, or you can open VS Code, and search for C/C++ in the extensions menu (Ctrl+Shift+X).

  4. If you want to open the terminal in VS Code, select View > Terminal, or alternatively use the shortcut Ctrl+` (using the backtick character). The default terminal is PowerShell.

Hello, world! tutorial (Rust with VS Code)

Let's take Rust for a spin with a simple 'Hello, world!' app.

  1. First, launch a command prompt (the x64 Native Tools Command Prompt for VS, or any cmd.exe), and cd to a folder where you want to keep your Rust projects.

  2. Then ask Cargo to create a new Rust project for you with the following command.

    The argument you pass to the cargo new command is the name of the project that you want Cargo to create. Here, the project name is first_rust_project. The recommendation is that you name your Rust projects using snake case (where words are lower-case, with each space replaced by an underscore).

    Cargo creates a project for you with the name that you supply. And in fact Cargo's new projects contain the source code for a very simple app that outputs a Hello, world! message, as we'll see. In addition to creating the first_rust_project project, Cargo has created a folder named first_rust_project, and has put the project's source code files in there.

  3. So now cd into that folder, and then launch VS Code from within the context of that folder.

  4. In VS Code's Explorer, open the src > main.rs file, which is the Rust source code file that contains your app's entry point (a function named main). Here's what it looks like.

    Note

    When you open the first .rs file in VS Code, you'll get a notification saying that some Rust components aren't installed, and asking whether you want to install them. Click Yes, and VS Code will install the Rust language server.

    You can tell from glancing at the code in main.rs that main is a function definition, and that it prints the string 'Hello, world!'. For more details about the syntax, see Anatomy of a Rust Program on the Rust website.

  5. Now let's try running the app under the debugger. Put a breakpoint on line 2, and click Run > Start Debugging (or press F5). There are also Debug and Run commands embedded inside the text editor.

    Note

    When you run an app under the debugger for the first time, you'll see a dialog box saying 'Cannot start debugging because no launch configuration has been provided'. Click OK to see a second dialog box saying 'Cargo.toml has been detected in this workspace. Would you like to generate launch configurations for its targets?'. Click Yes. Then close the launch.json file and begin debugging again.

  6. As you can see, the debugger breaks at line 2. Press F5 to continue, and the app runs to completion. In the Terminal pane, you'll see the expected output 'Hello, world!'.

Visual Studio Code Rust

Rust for Windows

Visual Studio Rust Project

Not only can you use Rust on Windows, you can also write apps for Windows using Rust. Via the windows crate, you can call any Windows API past, present, and future. There are more details about that, and code examples, in the Rust for Windows, and the windows crate topic.

Visual Studio Rust-analyzer

Related