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

77

我已经安装了Rust在Visual Studio Code中的扩展:

图片描述

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

图片描述

我尝试过点击Run TaskRun build taskConfigure Default build task,但没有任何合理的反应。


1
{btsdaf} - E net4
{btsdaf} - Stepan Yakovenko
1
@StepanYakovenko -> 哪一个?我总是使用官方工具...在这种情况下,我相信你截图中的第二个 - 即“Rust (rls)”。 - SilSur
5个回答

161

使用集成终端

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

cargo run

安装rust-analyzer基于LLDB的本地调试器扩展,然后使用运行菜单(然后查看终端的结果/输出)。

enter image description here

您可以使用终端命令安装这些扩展(然后重新启动 vscode):
# apt install musl lldb libssl-dev
code --install-extension vadimcn.vscode-lldb
code --install-extension rust-lang.rust-analyzer
code --install-extension tamasfe.even-better-toml

使用任务

运行任务的快捷方式: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 + P 并选择命令面板中的 Tasks: Run Build Task
您可以像上面的注释一样添加参数,例如:"args": ["run", "--release", "--", "arg1"](如果您的应用程序需要)。
(您可以使用 Ctrl + Shift + P 打开命令面板,并输入 Configure Default Build Task,然后按下 Enter 选择它。然后选择 Rust: cargo buildOthers。这将在您的工作区 .vscode 文件夹中生成一个 tasks.json 文件)。
使用基于LLDB的本地调试器
运行项目: 按下Ctrl+F5或从“运行”菜单中选择“无调试运行”,然后在终端窗口中查看结果。

Native debugger based on LLDB

首次(仅一次)安装基于LLDB的本地调试器,或使用命令行进行安装:
code --install-extension vadimcn.vscode-lldb

然后在您的Visual Studio Code项目中:按下快捷键Ctrl+F5,然后首次选择LLDB,然后选择OK和Yes,或者像以下示例一样创建.vscode/launch.json文件,在您的项目文件夹内(您也可以从Debug/Run面板中选择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
如果你需要参数,可以取消注释上面的// "user_arg1",

使用rust-analyzer扩展

安装

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

点击上面灰色的 Run 文本以运行代码,位于 fn main() 上方: the gray Run text above main, image

使用code-runner扩展

安装扩展,然后打开源文件,你将在右上角看到一个播放按钮,可以点击它,或者使用默认快捷键:Ctrl+Alt+N(你可以从文件>首选项>键盘快捷方式更改快捷键,并在搜索框中输入code-runner.run)。
注意:要在终端内运行命令,你可以从文件>首选项>设置(或按下Ctrl+,)将code-runner.runInTerminal设置为true,然后在搜索框中输入code-runner.runInTerminal
编辑:此操作仅对打开的文件有效,例如:rustc main.rs。你可以编辑code-runner.executorMap以更改命令。

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

给:

"rust": "cargo run",

因此,每次点击播放按钮(或按下键盘快捷键)时,Code Runner 都会运行 cargo run 命令:
从菜单中选择: 文件>首选项>设置(或按下 Ctrl+,),然后在搜索框中输入:
code-runner.executorMap 然后点击 在 Settings.json 中编辑 ,然后编辑 "code-runner.executorMap":和更改 "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"
菜单: 文件>首选项>设置(或按下Ctrl+,),然后在搜索框中输入customCommand并设置自定义命令运行:cargo run。 您可以将此命令的快捷方式更改为方便使用:从菜单中选择:文件>首选项>键盘快捷方式,然后在搜索框中输入:customCommand,然后添加/更改绑定键,例如按下:Ctrl+L Ctrl+R


使用rust-lang.rust扩展

您可以通过命令行安装此扩展:

code --install-extension rust-lang.rust

插件使用任务:您可以按下Ctrl + Shift + B,然后选择提供的选项,目前只有两个选项:
cargo check
cargo build

所以你需要使用上面介绍的任务(文件)。
kalitaalexey的'Rust'扩展以前是另一个选择,但在2021年被从Visual Studio Code Marketplace中移除,并且最后更新于2017年

3
Code Runner 似乎不是合适的工具,因为它忽略了我的项目依赖,并且只运行一个文件(并且失败了)。 - Stepan Yakovenko
谢谢,但是我不明白你所说的“Never scan the build output”设置是什么意思,我没有看到它。 - Stepan Yakovenko
1
很遗憾,您的“使用rust-lang.rust扩展”的指令不再适用。您需要使用Kip所描述的“type: shell”而不是“type: cargo”。 - Timmmm
我需要一个快捷方式来“终止当前进程并再次运行cargo run”,这样的东西存在吗? - Alper
可以将cargo.toml文件的位置指向子项目以运行子项目作为主要的cargo文件。我的意思是,在子文件夹中嵌套cargo.toml,因为我正在学习Rust并跳转到多个小样本项目。 - Ko Lynn

6

目前还没有很好的解决办法。基本上,您需要向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"
      ]
    }
  ]
}

A.R.建议添加另一个相同的条目,但使用“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.

相反,你可以添加一个"type": "shell"任务。但是这仍然不完美,因为由于某种原因,添加该任务意味着在按下Ctrl-Shift-B时根本不会出现cargo checkcargo build

我的解决方案是将它们全部更改为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"
    }
  ]
}

3

我创建新项目的快速方式:

  1. 初始化或克隆新的存储库
  2. 在 vsCode 中安装扩展(对我来说是 "Rust and Friends")
  3. 如果不存在,创建 crate(s):
  4. cargo new myNewApp
    cargo run
    
  5. 选择 main.rs(或其他 xxx.rs 源代码 rust 文件)
  6. 按下 F5

oohh noo! but no problem

6. 好的

next

准备好了!根据你的创建,这将生成一个基于你的 crates 的 launch.json 文件。

enter image description here

选择您的启动配置并按下F5键 playing


1
我能够使用VSC扩展Rust (rls),并使用AR帖子的修改版本使其工作:
"tasks": [
    {
        "type": "shell",
        "label": "cargo run",
        "command": "wsl",
        "args": [
            "--",
            "~/.cargo/bin/cargo",
             "run"
        ],
        "problemMatcher": [
            "$rustc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

0
如果您想在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"
      }
   ]
}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接