如何制作 VS Code 构建和 运行 Rust 程序?

How to make VS Code build and run Rust programs?

我一直在使用 VS Code,我想知道如何构建一个包含这些命令的 task.json 文件。 cargo build, cargo run [ARGS] cargo run --release -- [ARGS]

我试过在 task.json 上用 documentation 制作一个。我一直收到 No such subcommand 个错误。

样本:

{
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "cargo",

// The command is a shell script
"isBuildCommand": true,

// Show the output window only if unrecognized errors occur. 
"showOutput": "silent",

"tasks": [{
   "taskName": "run test",
   "version": "0.1.0",
   "command": "run -- --exclude-dir=node_modules C:/Users/Aaron/Documents/Github/",
   "isShellCommand": true,
   "showOutput": "always"
},
{
   "taskName": "run",
   "version": "0.1.0",
   "args": [  "--"
           , "--exclude-dir=node_modules"
           , "C:/Users/Aaron/Documents/Github/"
           ]
   "isShellCommand": true,
   "showOutput": "always"
}]
}

命令 属性 仅在顶层受支持。此外,必须通过 args 属性 传递参数。如果将它们放入命令中,则该命令将被视为名称中带有空格的命令。 运行 任务的示例如下所示:

{
    "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"
        }
    ]
}

这是我配置 tasks.json 文件的方式

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

输入构建命令(ctrl+shift+b)将构建和运行代码。

可能这些答案已经过时了,这是我的 tasks.json,它实现了 cargo 运行、cargo build 和 cargo 命令到 运行 当前打开的例子...

关键是指定问题匹配器,这样你就可以点击错误:

{
    // 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"
            ]
        },
    ]
}