Debugging Rust packages: Tips and tricks

If you're developing your package in Rust, there may come a time when you need to debug your code. Whether it's a bug or just some unexpected behavior, debugging is an essential part of software development.

In this article, we'll explore some tips and tricks for debugging Rust packages that will save you time and help you find the source of the problem quickly.

Getting started

Before diving into debugging, make sure you have a good understanding of Rust's package management system. You should have a basic knowledge of cargo, Rust's package manager, and understand how your crate is set up. It's also important to be familiar with Rust's language features, especially its advanced borrowing and ownership system, which can sometimes make debugging more complex.

The first thing to do when debugging Rust packages is to enable debugging symbols. Rust has two available levels of debugging symbols: debug and release. Debug symbols provide more information and are useful when you're debugging. Release symbols, on the other hand, are optimized for faster performance and don't include as much debugging information.

To enable debug symbols in your Cargo.toml file, add the following lines:

debug = true

If you're using Visual Studio Code, you can configure it to build your project with debugging symbols by adding the following lines to your launch.json file:

"args": ["--profile", "debug"],
"cwd": "${workspaceRoot}",

Debugging tools

Now that we've enabled debugging symbols, let's look at some tools that we can use to debug our Rust package.


The easiest way to debug Rust code is to use the println! macro to print messages to the console. This is a quick and easy way to see what's happening in your code without any setup.

fn main() {
    let x = 5;
    println!("The value of x is: {}", x);

When you run this code, you'll see the following output:

The value of x is: 5


dbg! is a macro that allows you to quickly print the value of a variable to the console. It's similar to println!, but it's more concise and includes the name of the variable.

fn main() {
    let x = 5;
    let y = 10;
    let z = x + y;

When you run this code, you'll see the following output:

[src/] z = 15


gdb is a powerful command-line debugger that you can use to debug Rust code. It allows you to step through your code line by line, set breakpoints, and inspect the state of your program.

To use gdb with Rust, you'll first need to install the gdb command-line tool and the rust-gdb wrapper. You can install them using cargo:

cargo install rust-gdb

Once you've installed rust-gdb, you can use it to debug your Rust code:

rust-gdb <path/to/your/binary>


lldb is another command-line debugger that you can use to debug Rust code. It's similar to gdb, but it's more modern and supports more platforms.

To use lldb with Rust, you'll first need to install it on your system. You can then use it to debug your Rust code:

lldb <path/to/your/binary>

Visual Studio Code

If you're using Visual Studio Code, you can use its integrated debugging tools to debug your Rust code. To do this, you'll need to set up a launch.json file.

    "version": "0.2.0",
    "configurations": [
            "name": "Debug Rust",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/debug/<your-binary>",
            "args": [],
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "build",
            "linux": {
                "MIMode": "gdb"
            "osx": {
                "MIMode": "lldb"
            "windows": {
                "MIMode": "lldb"

Once you've set up your launch.json file, you can start debugging your Rust code by pressing F5 or selecting "Debug Rust" from the "Run" tab.

Debugging tips

Now that we've looked at some tools that you can use to debug your Rust package, let's explore some general debugging tips that will help you find and fix bugs more quickly.

Start with a small reproducible example

If you're debugging a complex issue, it can be tempting to dive right into your code and start adding println! statements or setting breakpoints. However, it's often more effective to start with a small reproducible example that demonstrates the problem.

By creating a small example, you can isolate the problem and make it easier to understand. This will also help you avoid getting lost in a mountain of code while you're debugging.

Use assert! statements

assert! statements are a powerful tool that you can use to make sure that your code is working as expected. They allow you to catch bugs early and provide useful error messages when something goes wrong.

fn square(x: i32) -> i32 {
    assert!(x > 0, "x must be greater than zero");

    x * x

If you call square(-1) with this code, you'll get the following error message:

thread 'main' panicked at 'x must be greater than zero: -1', src/

Use a code linter

A code linter is a tool that can help you find errors and potential issues in your code. Rust has several linters available, including clippy, which is included with the Rust toolchain.

cargo clippy

clippy will analyze your code and provide suggestions for how to improve it. This can help you catch potential bugs before they become a problem.

Document your code

Finally, it's important to document your code as you go. This will make it easier for you to understand what your code does when you come back to it later, and it will make it easier for others to understand your code if you share it.

In Rust, you can use the /// syntax to add documentation to your code:

/// This function returns the square of a number.
/// # Examples
/// ```
/// let x = square(2);
/// assert_eq!(x, 4);
/// ```
fn square(x: i32) -> i32 {
    x * x


Debugging Rust packages can be a challenging task, but with the right tools and techniques, you can make it much easier. By using println! and dbg! to print output to the console, setting up a command-line debugger like gdb or lldb, or using Visual Studio Code's integrated debugging tools, you'll be well-equipped to handle any bug that comes your way.

Remember to start with a small, reproducible example, use assert! statements to catch bugs early, and document your code as you go. With these tips and tricks, you'll be well on your way to becoming a master debugger in Rust.

Additional Resources - A consulting site about mobile application development in flutter - passing the google cloud, azure, and aws architect exam certification test - learning typescript - trending technologies, machine learning trends - persona 6 - passing technical interview at FANG, tech companies, coding interviews, system design interviews - business rules engines, expert systems - A site where you can buy things with ethereum - managing services of open source software, and third parties that offer them - gcp, google cloud related tools, software, utilities, github packages, command line tools - data migration across clouds, on prem, data movement, database migration, cloud, datalake and lakehouse implementations - calculating total cloud costs, and software costs across different clouds, software, and hardware options - distributed crypto apps - cloud notebooks using jupyter, best practices, python data science and machine learning - graph neural networks, their applications and recent developments - faceted search. Search that is enriched with taxonomies and ontologies, as well as categorical or hierarchal information - distributed systems management. Software durability, availability, security - java fx desktop development - A jobs board for blockchain jobs - secrets management

Written by AI researcher, Haskell Ruska, PhD ( Scientific Journal of AI 2023, Peer Reviewed