Rust:
Using a debugger

How to:

Rust supports various debuggers, but a common one is gdb for GNU/Linux or lldb for macOS. You might also use rust-gdb or rust-lldb which are wrappers that pretty-print Rust values. Here’s a glimpse:

fn main() {
    let mut counter = 0;
    for _ in 0..5 {
        counter += 1;
        println!("Counter is at: {}", counter);
    }
}

To debug this, compile with debug info:

$ rustc -g counter.rs

Then run it in rust-gdb:

$ rust-gdb counter
(gdb) break main
(gdb) run
(gdb) print counter
$1 = 0
(gdb) continue
Counter is at: 1
(gdb) print counter
$2 = 1

Deep Dive

Debugging’s been around since ye olde times of punch cards, and its evolution has been a godsend. Rust provides its own tooling with integrations for GDB and LLDB due to the language’s system-level nature.

Alternatives for debugging Rust code include using integrated development environments (IDEs) with their built-in debuggers, which some find more intuitive. Popular ones include CLion with the Rust plugin or Visual Studio Code with the Rust extension.

As for implementation, Rust generates debug symbols that these debuggers understand, which is vital for stepping through the code, setting breakpoints, and inspecting variables without losing your marbles.

See Also