如何从Visual Studio代码启动Rust应用程序?

Ste*_*nko 22 rust rust-cargo visual-studio-code rust-language-server

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

在此输入图像描述

我想运行我的项目,我不明白在哪里点击.

在此输入图像描述

我尝试单击运行任务,运行构建任务,配置默认构建任务,但没有任何合理的发生.

was*_*mup 33

使用键盘(集成终端):

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


使用Code Runner

安装Code Runner扩展,然后打开源文件,然后您将在右上角有一个播放按钮单击,或使用默认快捷方式:( Ctrl +您可以从以下位置更改快捷方式:cargo runCtrl + Shift + B在搜索框中输入).
注意:要运行在终端的命令,你可以设置cargo run.vscode/tasks.jsoncargo run(或按.vscode/tasks.json),然后输入Ctrl + Shift + B搜索框.
编辑:这只运行打开文件,例如:Ctrl + Shift + P.您可以编辑Tasks: Run Build Task以更改命​​令:

cargo run
Run Code Online (Sandbox Code Playgroud)

至:

{
  // 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",
      "args": ["run"],
      // "args": ["run", "--release", "--", "arg1"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

因此,Code Runner "args": ["run", "--release", "--", "arg1"],每次单击"播放"按钮(或按键盘快捷键)时都会运行命令:
从菜单:( Ctrl + Shift + P或按Configure Default Build Task)然后在搜索框内,输入:
Enter然后单击,Rust: cargo build然后编辑Otherstasks.json.


使用Code Runner自定义命令

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


使用构建任务:

请参阅更新的文档:https://code.visualstudio.com/docs/editor/tasks

下一步是设置任务配置文件code-runner.runInTerminal.要执行此操作,请使用Ctrl+ Shift+ 打开命令选项板P并键入"配置默认构建任务",然后按Enter将其选中.然后选择"从模板创建tasks.json文件".选择"其他",因为我们要运行外部命令.这会rustc main.rs在工作区code-runner.executorMap文件夹中生成一个文件.要用于cargo run运行项目,请按如下所示更改内容:

"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
Run Code Online (Sandbox Code Playgroud)

在更复杂的环境中,可以有多个构建任务,因此在按下Ctrl+ Shift+ B("运行构建任务")后,它会提示您选择要执行的任务.此外,它允许您扫描输出以查找编译问题,因此从显示的列表中选择"从不扫描构建输出".


使用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",
Run Code Online (Sandbox Code Playgroud)

使用"rust": "cargo run"扩展名

使用Ctrl+ 安装P并输入"ext install vscode-rust".使用Ctrl+ Shift+ 运行P,键入"cargo",然后选择"Cargo:Run".

编辑:您可以为此命令添加快捷方式以便于使用:
从菜单中选择:settings.json,然后在搜索框内输入:"code-runner.customCommand": "cargo run",然后添加键绑定,例如按:File>Preferences>Settings,如果您在非RLS模式下使用此扩展,则在终端中运行货物命令:您可以Ctrl+,customCommand菜单中设置(或按cargo run)然后输入File>Preferences>Keyboard Shortcuts内部搜索框.

  • Code Runner似乎不是一个正确的工具,因为它忽略了我的项目依赖项并且只运行一个文件(并且失败). (2认同)

Tim*_*mmm 5

不幸的是,目前还没有一个好的解决方案。基本上,您必须向添加一个任务tasks.json,该任务的开始如下:

{
  // 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"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

AR建议添加另一个相同的条目,但是添加"subcommand": "run"但不起作用。您收到此错误:

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.
Run Code Online (Sandbox Code Playgroud)

相反,您可以添加"type": "shell"任务。然而,这仍然不是完美的,因为某种原因将这一任务的手段cargo check,并cargo build没有显示出来,当你按下Ctrl-Shift-B键都没有。

我的解决方案就是将它们也更改为Shell任务,因此您的整个工作tasks.json是:

{
  // 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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)