I have installed the Visual Studio Code extensions for Rust:
I want to run my project and I don't understand where to click.
I tried clicking Run Task, Run build task, Configure Default build task, but nothing reasonable happens.
I have installed the Visual Studio Code extensions for Rust:
I want to run my project and I don't understand where to click.
I tried clicking Run Task, Run build task, Configure Default build task, but nothing reasonable happens.
On
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
}
}
]
On
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"
}
]
}
On
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.
On
My fast way to create a new proyect:
init or clone new repo
On vsCode install extensions (for me "Rust and Friends")
Create crate(s) if not exists:
cargo new myNewApp
cargo run
Select main.rs (or other xxx.rs source code rust file)
Press F5
Ready! this create launch.json file based on yours crates.
Select your launch configuration and press F5

Using the integrated terminal
Run the following command in the integrated terminal:
TLDR:
Install rust-analyzer and Native debugger based on LLDB extensions, then use the Run menu (then see the terminal for the result/output):
You may install these extensions using the terminal commands (and then restart the vscode):
Using Tasks
Shortcut to run the Task:
Ctrl + Shift + BAdd
cargo runas a default Task: add.vscode/tasks.jsonfile to your project as follows, to usecargo runto run the project, change the contents of.vscode/tasks.jsonas follows:Now press
Ctrl + Shift + Bto run the Task, or PressCtrl + Shift + Pand selectTasks: Run Build Taskfrom 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 + Pand type inConfigure Default Build Taskand pressEnterto select it. Then selectRust: cargo buildorOthers. This generates atasks.jsonfile in your workspace.vscodefolder).Using the Native debugger based on LLDB
To Run the project:
Press Ctrl+F5 or select
Run Without Debuggingfrom theRunmenu, and see the terminal window, for the result:For the first time (only once), install the Native debugger based on LLDB, or install using the command line:
Then inside your Visual Studio Code project: Press shortcut Ctrl+F5 then for the first time select
LLDBthenOKandYes, or create.vscode/launch.jsonfile like the following sample, inside your project folder (Also you may selectcreate a launch.json filefrom Debug/Run panel too):Notes:
I named the project
exampleabove.You may uncomment above
// "user_arg1",if you need args.Using the rust-analyzer extension
Installation:
To run the code click on the gray
Runtext abovefn main():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 Shortcutsand entercode-runner.runin the search box).Note: To run the command inside terminal You may set
code-runner.runInTerminaltotruefromFile>Preferences>Settings(or pressCtrl+,), then entercode-runner.runInTerminalin the search box.Edit: This runs only open file e.g.:
rustc main.rs. You may edit thecode-runner.executorMapto change the command from:to:
So the Code Runner runs the
cargo runcommand each time you click the Play button (or pressing keyboard shortcut):From menu:
File>Preferences>Settings(or pressCtrl+,) then inside search box, enter:code-runner.executorMapthen clickEdit in Settings.jsonthen 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.jsonfile):Using the Code Runner custom command
You may set the custom command to run:
"code-runner.customCommand": "cargo run"Menu:
File>Preferences>Settings(or pressCtrl+,) then inside search box, entercustomCommandand 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+RUsing the
rust-lang.rustextensionYou may install this extension from the command line using:
The plugin uses tasks: You may press
Ctrl + Shift + Bthen select options presented, for now, there are only two options:So you need to use the
cargo runTask presented above (tasks.jsonfile).kalitaalexey's 'Rust' extension was previously another option but was removed from the Visual Studio Code Marketplace by 2021, and was last updated in 2017.