![https://i.imgur.com/KcRC8gU.png](https://i.imgur.com/KcRC8gU.png)
![https://i.imgur.com/2Wy02O5.png](https://i.imgur.com/2Wy02O5.png)
![https://i.imgur.com/9QfEy0D.png](https://i.imgur.com/9QfEy0D.png)
很遗憾,VS Code不能直接调试Rust :( 但是不用担心,只需要进行几个配置步骤即可完成 :)
Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux
Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true
},
{
"name": "(OSX) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo",
"args": [],
"cwd": "${workspaceRoot}",
}
]
}
Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings
cargo init
初始化了项目文件夹。(项目文件夹名称必须与Cargo.toml中的包名称相同。)cargo run
。Run >>> Add Configuration...
菜单项。在下拉菜单中选择 C/C++: (Windows) launch
选项。现在您将获得一个包含单个配置对象的launch.json 文件。program
属性为 "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"
;这取决于您的包名称与项目文件夹名称相同。 (我也改变了 cwd
属性为 "${workspaceFolder}"
,但我不确定它是否重要。) 为了更清晰,这是我目前在 launch.json
文件中的配置(preLaunchTask
属性是稍后使用的):{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"preLaunchTask": "rust: cargo build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
在这一点上,只要您已经至少构建了一次项目,您可以按下F5进行调试。
如果您希望F5在调试之前同时保存更改并重新构建项目,则还必须添加一个构建任务并配置它在调试开始之前运行。
为此,请通过打开Show All Commands
框(F1或Ctrl+Shift+p)添加构建任务并选择Tasks: Configure Task
。选择rust: cargo build
。它将在launch.json
旁边创建一个tasks.json
文件;默认值就足够了。我的文件看起来像这样:
{
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"command": "build",
"problemMatcher": [
"$rustc"
],
"group": "build",
"label": "rust: cargo build"
}
]
}
然后,要将所有东西连接起来,您只需要手动添加preLaunchTask
属性到启动配置中,并将其值设置为任务标签。例如:"preLaunchTask":"rust: cargo build"
,就像我上面的示例launch.json
中一样。
此时,每当您按下F5键,VsCode都会保存您的工作,重新构建您的项目,然后开始调试。
]
}```,但是当我将代码粘贴到列表中时,会将一堆条目标记为错误。然而,当我运行代码时收到的错误不同了——```“找不到任务‘rust: cargo build’。”```。将第二个代码添加到launch.json中无法解决这个问题。按下“仍要调试”按钮会显示配置的调试类型'cppvsdbg'不受支持。
- stunlockedlaunch.json
文件,使用Run - Add configuration...
。然而,该文件不正确,应该使用正确的名称<your program>
。这是错误的方法。{
// 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",
"program": "${workspaceFolder}/<your program>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
文档在这一点上有些简略。正确的方法是选择一个不同的文件夹,即项目的顶层hello_world
。 Cargo.toml
文件可用。
现在,当使用“运行-添加配置...”选项,并选择“LLDB”选项时-
Cargo.toml
文件可以被捡起 -
然后使用 Cargo.toml
文件来正确构建 launch.json
文件 -
{
// 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 'hello_world'",
"cargo": {
"args": [
"build",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'hello_world'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
运行 - 启动调试
和运行 - 无调试运行
都正常工作。