【问题标题】:How to make VS Code build and run Rust programs?如何让 VS Code 构建和运行 Rust 程序?
【发布时间】:2015-06-03 18:11:35
【问题描述】:

我一直在使用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"
}]
}

【问题讨论】:

标签: rust visual-studio-code


【解决方案1】:

可能这些答案已经过时了,这是我的tasks.json,它实现了cargo run、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"
            ]
        },
    ]
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 很好奇这是否仍然适用于任何人或已过时。在我的系统上它只会产生一个空白的error: 消息
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2020-04-30
      相关资源
      最近更新 更多