How to launch a Rust application from Visual Studio Code?

前端 未结 4 1316
余生分开走
余生分开走 2020-12-23 12:39

I have installed the Visual Studio Code extensions for Rust:

I want to run my project and I don\'t understand where to click.

I tried clic

4条回答
  •  悲&欢浪女
    2020-12-23 13:22

    Unfortunately there isn't a good solution at the moment. Basically you have to add a task to tasks.json, which begins like this:

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558 
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "type": "cargo",
          "subcommand": "check",
          "problemMatcher": [
            "$rustc"
          ]
        },
        {
          "type": "cargo",
          "subcommand": "build",
          "problemMatcher": [
            "$rustc"
          ]
        }
      ]
    }
    

    A.R. suggested adding another identical entry but with "subcommand": "run" but it doesn't work. You get this error:

    Error: The cargo task detection didn't contribute a task for the following configuration:
    {
        "type": "cargo",
        "subcommand": "run",
        "problemMatcher": [
            "$rustc"
        ]
    }
    The task will be ignored.
    

    Instead you can add a "type": "shell" task. However this still isn't perfect because for some reason adding that task means cargo check and cargo build don't show up when you press Ctrl-Shift-B at all.

    My solution is just to change those to shell tasks too, so your entire tasks.json is:

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558 
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "cargo check",
          "command": "cargo",
          "args": [
              "check"
          ],
          "problemMatcher": [
              "$rustc"
          ],
          "group": "build"
        },
        {
          "type": "shell",
          "label": "cargo build",
          "command": "cargo",
          "args": [
              "build"
          ],
          "problemMatcher": [
              "$rustc"
          ],
          "group": "build"
        },
        {
          "type": "shell",
          "label": "cargo run",
          "command": "cargo",
          "args": [
              "run"
          ],
          "problemMatcher": [
              "$rustc"
          ],
          "group": "build"
        }
      ]
    }
    

提交回复
热议问题