在集成终端中运行以下命令:
cargo run
# 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 build
或 Others
。这将在您的工作区 .vscode
文件夹中生成一个 tasks.json
文件)。
code --install-extension vadimcn.vscode-lldb
{
// 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",
。
安装:
rustup component add rust-src
code --install-extension rust-lang.rust-analyzer
# code --install-extension matklad.rust-analyzer
Run
文本以运行代码,位于 fn main()
上方:
安装扩展,然后打开源文件,你将在右上角看到一个播放按钮,可以点击它,或者使用默认快捷键: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.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
cargo run
”,这样的东西存在吗? - Alper目前还没有很好的解决办法。基本上,您需要向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"
]
}
]
}
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 check
和cargo 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"
}
]
}
"tasks": [
{
"type": "shell",
"label": "cargo run",
"command": "wsl",
"args": [
"--",
"~/.cargo/bin/cargo",
"run"
],
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
{
"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"
}
]
}
通过添加"--"
和弱引用,您可以将参数传递给您的应用程序。