While Coding in Python, I often do something like
test.py
x = []
breakpoint()
± |master U:5 ?:4 ✗| → python3 test.py
-> breakpoint()
(Pdb) x
[]
(Pdb) x.append(1)
(Pdb) x
[1]
Is it possible to execute the statement while debugging Rust?
use std::collections::HashMap;
fn main() {
let mut contacts = HashMap::new();
contacts.insert("Daniel", "798-1364");
contacts.insert("Ashley", "645-7689");
//set breakpoint here
}
So far, I can execute p contacts
in the debug console, the meaning of this output isn't straight to me. What if I want to know the outcome of println!("{:?}", contacts);
without writting this line of code in the source file.
And also I want to know the outcome of contacts.insert("Robert", "956-1742")
, If I execute expr contacts.insert("Robert", "956-1742")
in the debug console, it says error: no field named insert
.
In CLion, Rust debugging works out-of-the-box; in our other IDEs, IntelliJ Rust prompts you to install the Native Debugging plugin and downloads the debugger binary upon the first session. CLion supports two debugger backends, GDB and LLDB, which you can switch between. The other IDEs work with LLDB only.
Open main.rs and click the left gutter in the editor to set a break point on the println! line. It should display as a red dot. To start debugging, use either the Rust Analyzer: Debug command or select the Debug CodeLens about main() .
GDB has support for several languages, such as C/C++, but also modern languages such as Go and Rust.
LLDB provides a modern, high performance debugger framework and is the default debugger for macOS and iOS.
Rust debugging support is currently quite limited with both lldb and gdb. Simple expressions work, anything more complex is likely to cause problems.
My experience while testing this with rust-lldb
:
capacity()
on the HashMap
in the debugger since that function is not included in the binary.struct_value.method(&struct_value)
HashMap
)."abcdef"
is a const char [7]
including the trailing NUL byte. Thus they cannot be easily passed to Rust functions expecting &str
arguments.Trying to use a helper function like this:
pub fn print_contacts(contacts: &HashMap<&str, &str>) {
println!("{:?}", contacts);
}
causes lldb to crash:
(lldb) expr print_contacts(&contacts)
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0. Program arguments: [...]
Segmentation fault: 11
So it seems that what you want to do is not possible at this time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With