如何使VS代码构建和运行Rust程序?

时间:2015-06-03 18:11:35

标签: rust visual-studio-code

我一直在使用VS Code,我想知道如何构建一个具有这些命令的task.json文件。 cargo buildcargo 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"
}]
}

3 个答案:

答案 0 :(得分:3)

仅在顶级支持命令属性。此外,必须通过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"
        }
    ]
}

答案 1 :(得分:2)

以下是我配置tasks.json文件的方法

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

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

答案 2 :(得分:2)

可能这些答案已过时,这是我的task.json,它实现了货物运行,货物建造和货物命令来运行当前打开的示例...

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

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