官术网_书友最值得收藏!

How to do it...

Execute the following steps for this recipe:

  1. Create a new binary project to debug: cargo new debug-me. Open this project in Visual Studio Code with the new extension loaded.
  2. Before anything can happen, Visual Studio Code needs a launch configuration to recognize Rust's LLVM output. First, let's create this launch configuration; for that, add a .vscode directory containing a launch.json file to the project directory. This can be autogenerated, so make sure that launch.json contains the following:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'debug-me'",
"cargo": {
"args": [
"build",
"--bin=debug-me",
"--package=debug-me"
],
"filter": {
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'debug-me'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=debug-me",
"--package=debug-me"
],
"filter": {
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
  1. Now, let's open src/main.rs and add some code to debug:
struct MyStruct {
prop: usize,
}

struct Point(f32, f32);

fn main() {
let a = 42;
let b = vec![0, 0, 0, 100];
let c = [1, 2, 3, 4, 5];
let d = 0x5ff;
let e = MyStruct { prop: 10 };
let p = Point(3.14, 3.14);

println!("Hello, world!");
}

  1. Save and add a breakpoint in VS Code's user interface. Click left of the line numbers and a red dot should appear there. This is a breakpoint: 

  1. Having set a breakpoint, we expect the program to pause there and give us some insights into the current memory layout, that is, the state of any variables at that particular point in time. Run the debug launch configuration with F5 (or Debug | Start Debugging). The window configuration should change slightly and a panel on the left-hand side of the window shows local variables (among other things):
  1. Using the small control panel on top, you can then control the execution flow and watch the stack and memory on the left change accordingly. Note also the difference between an array and a (heap-allocated) vector!

Now, let's go behind the scenes to understand the code better.

主站蜘蛛池模板: 宾川县| 邓州市| 泽州县| 贵定县| 洛浦县| 株洲县| 博罗县| 桃江县| 香河县| 黔西县| 江津市| 名山县| 福安市| 宁国市| 拉孜县| 建宁县| 舟曲县| 秦安县| 景泰县| 寿阳县| 宝兴县| 右玉县| 禹城市| 湘潭市| 岱山县| 密云县| 洛扎县| 抚顺市| 镇巴县| 龙南县| 吴忠市| 莱芜市| 台东市| 土默特左旗| 泰兴市| 普安县| 斗六市| 双城市| 米易县| 兴仁县| 深州市|