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

问题描述 投票:16回答:3

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

enter image description here

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

enter image description here

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

rust visual-studio-code rust-cargo rls
3个回答
18
投票

Using the integrated terminal

在集成终端中运行以下命令:

cargo run

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


Using Tasks

您可以添加cargo run作为默认任务:使用Ctrl + Shift + P打开命令选项板并键入Configure Default Build Task并按Enter将其选中。然后选择Rust: cargo build。这会在工作区tasks.json文件夹中生成.vscode文件。要使用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", // "type": "cargo",
            "command": "cargo run", // "subcommand": "build",
            // "problemMatcher": ["$rustc"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

现在你可以按qazxsw poi来运行任务,或按qazxsw poi并从命令调色板中选择Ctrl + Shift + B


Using Code Runner

安装Ctrl + Shift + P扩展,然后打开源文件,然后您将在右上角有一个播放按钮单击,或使用默认快捷方式:Tasks: Run Build Task(您可以更改快捷方式:Code Runner并在搜索框中输入Ctrl+Alt+N)。 注意:要在终端内运行命令您可以从File>Preferences>Keyboard Shortcuts(或按code-runner.run)将code-runner.runInTerminal设置为true,然后在搜索框中输入File>Preferences>Settings。 编辑:这只运行打开文件,例如:Ctrl+,。您可以编辑code-runner.runInTerminal以更改命令:

rustc main.rs

至:

code-runner.executorMap

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

注意:我正在使用:

"code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt"

"rust": "cargo run"


Using Code Runner custom command

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


Using the customCommand extension

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

Ctrl+L Ctrl+R

该插件使用任务:您可以按rust-lang.rust然后选择显示的选项,现在只有两个选项:

code --install-extension rust-lang.rust

所以你需要使用上面提到的Ctrl + Shift + B任务。


Using the cargo check cargo build extension

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

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


3
投票

不幸的是,目前还没有一个好的解决方案。基本上你必须向Ctrl+,添加一个任务,它的开头是这样的:

executeCargoCommandInTerminal

A.R.建议添加另一个相同的条目,但与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"
      ]
    }
  ]
}

相反,你可以添加一个"subcommand": "run"任务。然而,这仍然不完美,因为由于某种原因添加该任务意味着当您按Ctrl-Shift-B时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. "type": "shell"不显示。

我的解决方案只是将那些更改为shell任务,所以你的整个cargo check是:

cargo build

2
投票

我能够使用VSC扩展,tasks.json,使用AR的帖子的修改版本来使用它:

{
  // 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"
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.