- Rust Programming Cookbook
- Claus Matzinger
- 361字
- 2021-06-24 12:27:47
How to do it...
Execute the following steps for this recipe:
- Create a new binary project to debug: cargo new debug-me. Open this project in Visual Studio Code with the new extension loaded.
- 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}"
}
]
}
- 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!");
}
- 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:

- 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):

- 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.
推薦閱讀
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- C# 2012程序設計實踐教程 (清華電腦學堂)
- 深入實踐Spring Boot
- Twilio Best Practices
- Apache Karaf Cookbook
- Microsoft Dynamics GP 2013 Reporting, Second Edition
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Extending Puppet(Second Edition)
- 劍指大數據:企業級數據倉庫項目實戰(在線教育版)
- ASP.NET開發與應用教程
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- Mastering Concurrency Programming with Java 9(Second Edition)
- Python Linux系統管理與自動化運維
- 計算機系統解密:從理解計算機到編寫高效代碼
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)