57

我已经为 Rust 安装了 Visual Studio Code 扩展:

在此处输入图像描述

我想运行我的项目,但我不知道在哪里单击。

在此处输入图像描述

我尝试单击Run TaskRun build taskConfigure Default build task,但没有发生任何合理的事情。

4

4 回答 4

122

使用集成终端

运行集成终端的快捷方式:(Ctrl + `
Ctrl + backtick
然后在集成终端中运行以下命令:

cargo run

注意:从项目文件夹中打开代码编辑器(code .项目文件夹终端内的命令,或在 GUI 模式下:在项目文件夹内右键单击并选择Open With Code)然后按Ctrl + `(Ctrl +反引号)打开集成终端,然后输入:cargo run


TLDR : 安装rust-analyzer基于 LLDB 的 Native 调试器

或者使用终端命令安装:

code --install-extension matklad.rust-analyzer
code --install-extension vadimcn.vscode-lldb

使用任务

运行任务的快捷方式:Ctrl + Shift + B
添加cargo run为默认任务:将.vscode/tasks.json文件添加到您的项目中,如下所示,用于cargo run运行项目,更改内容.vscode/tasks.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",
            "type": "shell",
            "command": "~/.cargo/bin/cargo", // note: full path to the cargo
            "args": [
                "run",
                // "--release",
                // "--",
                // "arg1"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

现在按下Ctrl + Shift + B以运行任务,或按下Ctrl + Shift + PTasks: Run Build Task从命令面板中选择。

您可以添加类似上述评论的参数,例如:("args": ["run", "--release", "--", "arg1"],如果您的应用需要)。

(您可以打开命令面板Ctrl + Shift + P并输入Configure Default Build Task并按下Enter以选择它。然后选择Rust: cargo build或。这会在您的工作区文件夹中Others生成一个文件)。tasks.json.vscode


使用基于 LLDB 的 Native 调试器

运行项目:
Ctrl+F5Run Without DebuggingRun菜单中选择,然后查看终端窗口,查看结果:

基于 LLDB 的原生调试器

第一次(仅一次),安装基于 LLDB 的 Native 调试器,或者使用命令行安装:

code --install-extension vadimcn.vscode-lldb

然后在您的 Visual Studio Code 项目中:按快捷键Ctrl+F5,然后第一次选择LLDBthen OKand Yes或者在您的项目文件夹中创建.vscode/launch.json如下示例的文件(您也可以create a launch.json file从调试/运行面板中选择):

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'example'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [
                // "user_arg1",
                // "user_arg2"
            ],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'example'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

笔记:
我在上面命名了这个项目example。如果您需要 args
,您可以取消上面的注释。// "user_arg1",


使用rust-analyzer扩展

安装

rustup component add rust-src
code --install-extension matklad.rust-analyzer

运行代码,请单击上面的灰色Run文本fn main()主图像上方的灰色运行文本


使用代码运行器扩展

安装扩展,然后打开源文件,右上角会有一个播放按钮可以点击,或者使用默认快捷方式:(Ctrl+Alt+N您可以更改快捷方式:File>Preferences>Keyboard Shortcutscode-runner.run在搜索框中输入)。
注意:要在终端中运行命令,您可以设置code-runner.runInTerminaltruefrom File>Preferences>Settings(或按Ctrl+,),然后code-runner.runInTerminal在搜索框中输入。
编辑:这仅运行打开的文件,例如:rustc main.rs. 您可以编辑code-runner.executorMap以更改命令:

"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",

至:

"rust": "cargo run",

cargo run因此,每次单击“播放”按钮(或按键盘快捷键)时,代码运行器都会运行命令:
从菜单:(File>Preferences>Settings或按Ctrl+,)然后在搜索框中,输入:
code-runner.executorMap然后单击Edit in Settings.json然后编辑"code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt""rust": "cargo run"

或者简单地将以下 3 行添加到 VSCode 设置 JSON(settings.json文件):

"code-runner.executorMap": {
  "rust": "cargo run # $fileName"
}

使用 Code Runner 自定义命令

您可以将自定义命令设置为运行:"code-runner.customCommand": "cargo run"
菜单:(File>Preferences>Settings或按Ctrl+,)然后在搜索框内,输入customCommand并设置自定义命令运行:cargo run。您可以将快捷方式更改为此命令以方便使用:从菜单选择:File>Preferences>Keyboard Shortcuts,然后在搜索框中输入:customCommand,然后添加/更改键绑定,例如按:Ctrl+L Ctrl+R


使用rust-lang.rust扩展

您可以使用以下命令从命令行安装此扩展:

code --install-extension rust-lang.rust

该插件使用任务:您可以按Ctrl + Shift + B然后选择出现的选项,目前只有两个选项:

cargo check
cargo build

所以你需要使用cargo run上面介绍的任务(tasks.json文件)。


使用vscode-rust扩展

使用Ctrl+安装P并输入“ext install vscode-rust”。Ctrl使用++运行,输入“cargo”,然后选择“Cargo:Run” ShiftP

编辑:您可以为该命令添加快捷方式以方便使用:
从菜单选择:File>Preferences>Keyboard Shortcuts,然后在搜索框中输入:Cargo:Run,然后添加键绑定,例如按:Ctrl+L Ctrl+R,如果您在非 RLS 模式下使用此扩展程序在终端中运行 Cargo 命令:您可以"rust.executeCargoCommandInTerminal": trueFile>Preferences>Settings菜单中设置(或按Ctrl+,)然后executeCargoCommandInTerminal在搜索框中输入。

于 2017-10-23T09:01:22.183 回答
7

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"
    }
  ]
}
于 2019-06-03T13:59:47.723 回答
2

我能够使用 VSC 扩展Rust (rls)使用 AR 帖子的修改版本来完成这项工作:

"tasks": [
    {
        "type": "shell",
        "label": "cargo run",
        "command": "wsl",
        "args": [
            "--",
            "~/.cargo/bin/cargo",
             "run"
        ],
        "problemMatcher": [
            "$rustc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
于 2019-04-14T01:03:30.627 回答
1

如果您想在 Visual Studio Code 中使用命令行参数运行 Rust 应用程序,您可以通过以下方式配置您的任务:

{
   "label":"Run With Arguments",
   "type":"process",
   "command":"cargo",
   "group":"none",
   "args":[
      "run",
      {
         "value":"--",
         "quoting":"weak"
      },
      {
         "value":"--argumentOne=\"Something\"",
         "quoting":"weak"
      },
      {
         "value":"--argumentTwo=\"Something\"",
         "quoting":"weak"
      }
   ]
}

通过添加"--"和弱引用,您可以将参数传递给您的应用程序。

于 2019-09-24T18:16:21.040 回答