Monday, May 3, 2021

How to Debug Rust in Visual Studio Code (Guide)

 

 

 


 

Assume that you have installed Rust in your computer. If you need guidance on how to install and configure Rust, you can find it here and of course Visual Studio Code installed as well.

Configuring Visual Studio Code for Rust development isn't difficult. Hopefully this guidance is useful to help you in Rust development. Let's start.


 Install Visual Studio Code Extension

1. You need to install extension according to you platform
             - C/C++  (for Windows)
             - CodeLLDB (for MacOS/Linux)

   since I used Ubuntu Linux then I installed CodeLLDB as shown as below

                      


2. Install Rust (rls) - For Auto Complete



 

 3. Install Rust Analyzer

 


 

 

Debugging The Code

 Now, you can use VS Code for Rust development. Let's try by creating new project via console

❯ cargo new helloworld
Created binary (application) `helloworld` package

Modify main.rs code by typing

fn perkalian(x: i32)-> i32 {
    x * x
}
fn main() {
    println!("Hello, world!");
    let number:i32 = 4;
    println!("Result = {}", perkalian(number));
}

You can watch variable number by right clicking then add to watch Create your a launch.json using lldb by pressing Ctrl+Shift+P and select Debug: Open launch.json Just copy and paste the script line below

{
      "version": "0.2.0",
      "configurations": [
      {
              "type": "lldb",
              "request": "launch",
              "name": "helloworld",
              "args": [],
              "program": "${workspaceFolder}/target/debug/helloworld",
              "windows": {
                    "program": "${workspaceFolder}/target/debug/helloworld.exe"
              },
              "cwd": "${workspaceFolder}",
              "stopOnEntry": false,
              "sourceLanguages": [
                    "rust"
              ]
        }
      ]
}


Now, you have to build and run with lldb debugger attached.

    • To build: Press Ctrl + Shift + B
    • Toggle breakpoints: F9
    • To debug: Press F5


 

 That's all. Thank you 

 

3 comments: