我已经为 Rust 安装了 Visual Studio Code 扩展:
我想运行我的项目,但我不知道在哪里单击。
我尝试单击Run Task、Run build task、Configure Default build task,但没有发生任何合理的事情。
我已经为 Rust 安装了 Visual Studio Code 扩展:
我想运行我的项目,但我不知道在哪里单击。
我尝试单击Run Task、Run build task、Configure Default build task,但没有发生任何合理的事情。
运行集成终端的快捷方式:(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 + P
并Tasks: Run Build Task
从命令面板中选择。
您可以添加类似上述评论的参数,例如:("args": ["run", "--release", "--", "arg1"],
如果您的应用需要)。
(您可以打开命令面板Ctrl + Shift + P
并输入Configure Default Build Task
并按下Enter
以选择它。然后选择Rust: cargo build
或。这会在您的工作区文件夹中Others
生成一个文件)。tasks.json
.vscode
运行项目:
按Ctrl+F5或Run Without Debugging
从Run
菜单中选择,然后查看终端窗口,查看结果:
第一次(仅一次),安装基于 LLDB 的 Native 调试器,或者使用命令行安装:
code --install-extension vadimcn.vscode-lldb
然后在您的 Visual Studio Code 项目中:按快捷键Ctrl+F5,然后第一次选择LLDB
then OK
and 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",
安装:
rustup component add rust-src
code --install-extension matklad.rust-analyzer
安装扩展,然后打开源文件,右上角会有一个播放按钮可以点击,或者使用默认快捷方式:(Ctrl+Alt+N
您可以更改快捷方式:File>Preferences>Keyboard Shortcuts
并code-runner.run
在搜索框中输入)。
注意:要在终端中运行命令,您可以设置code-runner.runInTerminal
为true
from 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.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” Shift。P
编辑:您可以为该命令添加快捷方式以方便使用:
从菜单选择:File>Preferences>Keyboard Shortcuts
,然后在搜索框中输入:Cargo:Run
,然后添加键绑定,例如按:Ctrl+L Ctrl+R
,如果您在非 RLS 模式下使用此扩展程序在终端中运行 Cargo 命令:您可以"rust.executeCargoCommandInTerminal": true
在File>Preferences>Settings
菜单中设置(或按Ctrl+,
)然后executeCargoCommandInTerminal
在搜索框中输入。
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"
}
]
}
我能够使用 VSC 扩展Rust (rls)使用 AR 帖子的修改版本来完成这项工作:
"tasks": [
{
"type": "shell",
"label": "cargo run",
"command": "wsl",
"args": [
"--",
"~/.cargo/bin/cargo",
"run"
],
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
如果您想在 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"
}
]
}
通过添加"--"
和弱引用,您可以将参数传递给您的应用程序。