How to make VS Code build and run Rust programs?

Here is how I configured my tasks.json file

{
    "version": "0.1.0",
    "command": "cargo",
    "isShellCommand": true,
    "args": ["run"],
    "showOutput": "always"
}

Entering the build command (ctrl+shift+b) will build and run the code.


The command property is only supported at the top level. In addition, arguments have to be passed via the args property. If they are put into the command, the command is treated as a command with whitespaces in its name. An example of the run task would look like this:

{
    "version": "0.1.0",
    "command": "cargo",
    "isShellCommand": true, // Only needed if cargo is a .cmd file
    "tasks": [
        {
           "taskName": "run",
           "args": [
               "--release"
               // More args
           ],
           "showOutput": "always"
        }
    ]
}

Possibly these answers are out of date, here is my tasks.json, which implements cargo run, cargo build and a cargo command to run the currently open example...

A key thing is to specify the problem matcher, so you can click through errors:

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