How to launch a Rust application from Visual Studio Code?

RustVisual Studio-CodeRust CargoRls

Rust Problem Overview


I have installed the Visual Studio Code extensions for Rust:

enter image description here

I want to run my project and I don't understand where to click.

enter image description here

I tried clicking Run Task, Run build task, Configure Default build task, but nothing reasonable happens.

Rust Solutions


Solution 1 - Rust

Using the integrated terminal

Run the following command in the integrated terminal:

cargo run

TLDR:
Install rust-analyzer and Native debugger based on LLDB extensions, then use the Run menu (then see the terminal for the result/output):

enter image description here

You may install these extensions using the terminal commands (and then restart the vscode):

code --install-extension matklad.rust-analyzer
code --install-extension vadimcn.vscode-lldb

Using Tasks

Shortcut to run the Task: Ctrl + Shift + B
Add cargo run as a default Task: add .vscode/tasks.json file to your project as follows, to use cargo run to run the project, change the contents of .vscode/tasks.json as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo run",
            "type": "shell",
            "command": "~/.cargo/bin/cargo", // note: full path to the cargo
            "args": [
                "run",
                // "--release",
                // "--",
                // "arg1"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Now press Ctrl + Shift + B to run the Task, or Press Ctrl + Shift + P and select Tasks: Run Build Task from the Command Palette.

You may add arguments like the comment above e.g.: "args": ["run", "--release", "--", "arg1"], (if your app requires it).

(You may open the Command Palette with Ctrl + Shift + P and type in Configure Default Build Task and press Enter to select it. Then select Rust: cargo build or Others. This generates a tasks.json file in your workspace .vscode folder).


Using the Native debugger based on LLDB

To Run the project:
Press Ctrl+F5 or select Run Without Debugging from the Run menu, and see the terminal window, for the result:

Native debugger based on LLDB

For the first time (only once), install the Native debugger based on LLDB, or install using the command line:

code --install-extension vadimcn.vscode-lldb

Then inside your Visual Studio Code project: Press shortcut Ctrl+F5 then for the first time select LLDB then OK and Yes, or create .vscode/launch.json file like the following sample, inside your project folder (Also you may select create a launch.json file from Debug/Run panel too):

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'example'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [
                // "user_arg1",
                // "user_arg2"
            ],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'example'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

Notes:
I named the project example above.
You may uncomment above // "user_arg1", if you need args.


Using the rust-analyzer extension

Installation:

rustup component add rust-src
code --install-extension matklad.rust-analyzer

To run the code click on the gray Run text above fn main(): the gray Run text above main, image


Using the code-runner extension

Install the extension, then open the source file then you will have a play button in the top right corner to click, or use default shortcut: Ctrl+Alt+N (You may change the shortcut from: File>Preferences>Keyboard Shortcuts and enter code-runner.run in the search box).
Note: To run the command inside terminal You may set code-runner.runInTerminal to true from File>Preferences>Settings (or press Ctrl+,), then enter code-runner.runInTerminal in the search box.
Edit: This runs only open file e.g.: rustc main.rs. You may edit the code-runner.executorMap to change the command from:

"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",

to:

"rust": "cargo run",

So the Code Runner runs the cargo run command each time you click the Play button (or pressing keyboard shortcut):
From menu: File>Preferences>Settings (or press Ctrl+,) then inside search box, enter:
code-runner.executorMap then click Edit in Settings.json then edit "code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt" to "rust": "cargo run".

Or simply add 3 following lines to VSCode settings JSON (settings.json file):

"code-runner.executorMap": {
  "rust": "cargo run # $fileName"
}

Using the Code Runner custom command

You may set the custom command to run: "code-runner.customCommand": "cargo run"
Menu: File>Preferences>Settings (or press Ctrl+,) then inside search box, enter customCommand and set the custom command to run: cargo run. You may change Shortcut to this command for ease of use: From Menu select: File>Preferences>Keyboard Shortcuts, then inside search box enter: customCommand, then add/change keybinding e.g. press: Ctrl+L Ctrl+R


Using the rust-lang.rust extension

You may install this extension from the command line using:

code --install-extension rust-lang.rust

The plugin uses tasks: You may press Ctrl + Shift + B then select options presented, for now, there are only two options:

cargo check
cargo build

So you need to use the cargo run Task presented above (tasks.json file).


Using the vscode-rust extension

Install with Ctrl+P and type "ext install vscode-rust". Run with Ctrl+Shift+P, type "cargo" then select "Cargo:Run".

Edit: You may add Shortcut to this command for ease of use:
From Menu select: File>Preferences>Keyboard Shortcuts, then inside search box enter: Cargo:Run, then add keybinding e.g. press: Ctrl+L Ctrl+R, and if you are using this extension in non RLS mode to run Cargo command in terminal: you may set "rust.executeCargoCommandInTerminal": true in File>Preferences>Settings menu (or press Ctrl+,) then enter executeCargoCommandInTerminal inside search box.

Solution 2 - Rust

Unfortunately there isn't a good solution at the moment. Basically you have to add a task to tasks.json, which begins like this:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558 
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cargo",
      "subcommand": "check",
      "problemMatcher": [
        "$rustc"
      ]
    },
    {
      "type": "cargo",
      "subcommand": "build",
      "problemMatcher": [
        "$rustc"
      ]
    }
  ]
}

A.R. suggested adding another identical entry but with "subcommand": "run" but it doesn't work. You get this error:

Error: The cargo task detection didn't contribute a task for the following configuration:
{
    "type": "cargo",
    "subcommand": "run",
    "problemMatcher": [
        "$rustc"
    ]
}
The task will be ignored.

Instead you can add a "type": "shell" task. However this still isn't perfect because for some reason adding that task means cargo check and cargo build don't show up when you press Ctrl-Shift-B at all.

My solution is just to change those to shell tasks too, so your entire tasks.json is:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558 
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "cargo check",
      "command": "cargo",
      "args": [
          "check"
      ],
      "problemMatcher": [
          "$rustc"
      ],
      "group": "build"
    },
    {
      "type": "shell",
      "label": "cargo build",
      "command": "cargo",
      "args": [
          "build"
      ],
      "problemMatcher": [
          "$rustc"
      ],
      "group": "build"
    },
    {
      "type": "shell",
      "label": "cargo run",
      "command": "cargo",
      "args": [
          "run"
      ],
      "problemMatcher": [
          "$rustc"
      ],
      "group": "build"
    }
  ]
}

Solution 3 - Rust

I was able to get this working using the VSC extension, Rust (rls), using a modified version of AR's post:

"tasks": [
    {
        "type": "shell",
        "label": "cargo run",
        "command": "wsl",
        "args": [
            "--",
            "~/.cargo/bin/cargo",
             "run"
        ],
        "problemMatcher": [
            "$rustc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

Solution 4 - Rust

My fast way to create a new proyect:

  1. init or clone new repo

  2. On vsCode install extensions (for me "Rust and Friends")

  3. Create crate(s):

    cargo new myNewApp
    cargo run
    
  4. Select main.rs (or other xxx.rs source code rust file)

  5. Press F5

oohh noo! but no problem

  1. OK

next

  1. YES

Ready! this create launch.json file based on yours creates.

enter image description here

Select your launch configuration and press F5 playing

呂 呂 呂 呂

Solution 5 - Rust

If you want to run a Rust application in Visual Studio Code with command line arguments, you can configure your task in this way:

{
   "label":"Run With Arguments",
   "type":"process",
   "command":"cargo",
   "group":"none",
   "args":[
      "run",
      {
         "value":"--",
         "quoting":"weak"
      },
      {
         "value":"--argumentOne=\"Something\"",
         "quoting":"weak"
      },
      {
         "value":"--argumentTwo=\"Something\"",
         "quoting":"weak"
      }
   ]
}

With the addition of "--" and weak quoting, you can pass arguments to your application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStepan YakovenkoView Question on Stackoverflow
Solution 1 - RustwasmupView Answer on Stackoverflow
Solution 2 - RustTimmmmView Answer on Stackoverflow
Solution 3 - RustKipView Answer on Stackoverflow
Solution 4 - RustBarrretttView Answer on Stackoverflow
Solution 5 - Rustlars.aView Answer on Stackoverflow