Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RUST cargo run task with arguments in vscode

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"
   ]
}
like image 380
Dr.YSG Avatar asked Feb 24 '20 17:02

Dr.YSG


People also ask

How do you pass arguments in VS Code?

Command line arguments In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do you use cargo in VS Code?

Cargo can be used to build your Rust project. Open a new VS Code integrated terminal (Ctrl+Shift+`) and type cargo build . You will now have target\debug folder with build output include an executable called hello_world.exe .

How do you run a Rust program in VS Code?

First, download and install Visual Studio Code for Windows. After you've installed VS Code, install the rust-analyzer extension. You can either install the rust-analyzer extension from the Visual Studio Marketplace, or you can open VS Code, and search for rust-analyzer in the extensions menu (Ctrl+Shift+X).

Is VS Code good for Rust?

Visual Studio Code (VS Code) Visual Studio Code is an open source and free-for-use project created by Microsoft and available for macOS, Windows, and Linux. It supports a lot of major languages, including Rust. Currently, VS Code is one of the best code editors around and the most-used editor for Rust development.


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}"
            ]
        }
    ]
}
like image 72
Benjamin Pannell Avatar answered Sep 19 '22 07:09

Benjamin Pannell