在集成终端中运行以下命令:
cargo run
注意:从项目文件夹中打开代码编辑器(项目文件夹终端内的code .
命令,或者在GUI模式下:右键单击项目文件夹并选择Open With Code
)然后按Ctrl +
`(Ctrl + backtick)打开集成终端,然后输入:cargo run
您可以添加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
。
安装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.executorMap
到Edit in Settings.json
。
注意:我正在使用:
"code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt"
见"rust": "cargo run"
您可以将自定义命令设置为运行:"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
customCommand
extension您可以使用以下命令从命令行安装此扩展:
Ctrl+L Ctrl+R
该插件使用任务:您可以按rust-lang.rust
然后选择显示的选项,现在只有两个选项:
code --install-extension rust-lang.rust
所以你需要使用上面提到的Ctrl + Shift + B
任务。
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
。
不幸的是,目前还没有一个好的解决方案。基本上你必须向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
我能够使用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"
}
]
}