Setup Rust development environment with VS Code

Here’s what I did to start developing in Rust on my Linux Manjaro laptop.

TL;DR – follow this guide, I should have done that from the start. Then come back here to setup the debugger.

I followed the installation instructions on the Rust website.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

I followed the prompts. Then I ran the following command to add the Rust binaries to the PATH. Alternatively you could close and reopen your terminal.

source "$HOME/.cargo/env"

I opened Visual Studio Code and installed the official Rust extension, rust-analyzer.

I was getting the error rust-analyzer failed to discover workspace. I followed this answer and initialised a Rust project called backend with

cargo new backend

then restarted VS Code. Then the extension worked.

Debugging

I installed the extension vadimcn.vscode-lldb.

I found the VS Code setting “Allow breakpoints everywhere” and ticked it.

I created the file .vscode/launch.json with the following contents which I adapted from here:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Rust application",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceRoot}/target/debug/backend",
            "cwd": "${workspaceRoot}",
        },
    ]
}

Then I could set breakpoints and have the debugger stop on them.

Related Posts

Leave a Reply

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