RUST cargo run task with arguments in vscode

Viewed 3038

Is there a way to specify arguments to a RUST cargo command running as VS CODE task? Or should I be trying this as an NPM script? (of course, this is RUST, so I am using CARGO and npm, creating a package.json would be odd).

The Build task works fine:

"version": "2.0.0",
"tasks": [
    {
      "type": "cargo",
      "subcommand": "build",
      "problemMatcher": [
        "$rustc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
},

But I have no idea where to put the arguments since I want it to be

$cargo run [filename]

{
  "type": "cargo",
  "subcommand": "run",
  "problemMatcher": [
    "$rustc"
   ]
}
1 Answers

There absolutely is, the args option allows you to pass additional arguments to your tasks and you can use various ${template} parameters to pass things like the currently opened file.

It's also worth calling out that the type of the command should probably be shell, with cargo being specified as the command itself.

For your use case, you may consider using the following (to execute cargo run $currentFile).

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run",
            "type": "shell",
            "problemMatcher": [
                "$rustc"
            ],
            "command": "cargo",
            "args": [
                "run",
                "${file}"
            ]
        }
    ]
}
Related