Why you should know Rust
According to Stackoverflow, Rust is the most loved language among developers. It is as fast as C/C++, but it ensures memory safety.
But generally speaking, Rust is said to be relatively difficult language to learn. Maybe if you know how to debug Rust programs, it might be easier to understand Rust.
By the way you can run codes written on this post on Rust playground and see how it is like.
Print debug
You can see detail of variable/struct/enum etc by println!("{:?}", variable);
pub fn main() {
let test = vec![100, 101, 102, 103];
println!("{:?}", test);
}
Result of this code:
[100, 101, 102, 103]
If you want to use this {:?} for struct, you need to add debug attribute.
pub fn main() {
let test = Person { name: "test", age: 20 };
println!("{:?}", test);
}
#[derive(Debug)] // Add this
struct Person<'a> {
name: &'a str,
age: u8
}
Result of this code:
Person { name: "test", age: 20 }
Same for enum:
pub fn main() {
let test = Fruits::Apple("ringo".to_string());
println!("{:?}", test);
}
#[derive(Debug)] // Add this.
enum Fruits {
Apple(String),
Grape(String),
Orange(String)
}
Result of this code:
Apple("ringo")
dbg! macro
dbg! macro can be used in Rust1.32.0 onward. This prints what file the operation is on and in which line the operation is.
pub fn main() {
let people = [
Person { name: "test1", age: 20 },
Person { name: "test2", age: 25 },
Person { name: "test3", age: 30 },
];
dbg!(&people);
}
#[derive(Debug)]
struct Person<'a> {
name: &'a str,
age: u8
}
Result of this code:
[src/main.rs:7] &people = [
Person {
name: "test1",
age: 20,
},
Person {
name: "test2",
age: 25,
},
Person {
name: "test3",
age: 30,
},
]
Print debug info only in debug build
This macro which prints debug info only in debug build was provided in stackoverflow. You can test it in Playground. This code below is from the stackoverflow answer.
#[cfg(debug_assertions)]
macro_rules! debug {
($x:expr) => { dbg!($x) }
}
#[cfg(not(debug_assertions))]
macro_rules! debug {
($x:expr) => { std::convert::identity($x) }
}
fn main() {
let x = 4;
debug!(x);
if debug!(x == 5) {
println!("x == 5");
} else {
println!("x != 5");
}
}
Use breakpoints in VSCode
Rust can use breakpoints
in vscode. The following is from stackoverflow answer.
- Install VS Code.
- Add "Rust" or "rust-analyzer" extension in your VSCode.
- Add "CodeLLDB" extension in your VSCode.
- Click on the debug icon in your VSCode, then click on "add launch.json". Select LLDB from the list of debug targets.
- Then .json file is added inside .vscode folder in the root directory of your project.
- Open the .json file and change the inside to the following:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Rust Debug Launch",
"program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}",
"args": [],
"cwd": "${workspaceRoot}/target/debug/",
"sourceLanguages": ["rust"]
}
]
}
For your reference, in my project, I am using the following .json file. You can see there are some argument information in "args" property.
{
"version": "0.2.0",
"configurations": [
{
"name": "Rust Debug Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}",
"args": ["--file", "${workspaceRoot}/examples/test.orn"],
"cwd": "${workspaceRoot}/target/debug/",
"sourceLanguages": ["rust"]
},
],
}
Then set some breakpoints in your VSCode and run "Rust Debug Launch" as follows:
Top comments (2)
Great article! I learnt about the dbg macro and conditional debugging 😁
Along with these, as rust compiles to binary, one can also use gdb as one would use on c programs to debug it...but I guess vs code breakpoints would be easier...😅
Thank you for the information, I didn't know it :)