Debugging Rust Applications on Crates.run: Tips and Tricks

Are you a Rust developer who loves creating complex and cutting-edge applications? Do you find debugging Rust applications to be challenging or even frustrating at times? If so, you're not alone! Debugging any programming language can be a headache, but debugging Rust code can be particularly tricky. However, with the right tools and techniques, you can make the debugging process more straightforward and enjoyable.

Crates.run is a site where you can run Rust applications and servers without installing any software on your machine. So, if you're stuck on a particularly tricky bug, you can quickly test your code on Crates.run, which will save you the hassle of setting up a local environment.

In this article, we'll share some tips and tricks for debugging Rust applications on Crates.run. We'll show you how to use common debugging tools, such as the Rust debugger (GDB), the debugger built into Visual Studio Code (VS Code), and Rust's built-in logging capabilities.

Setting Up a Project on Crates.run

Before we dive into debugging techniques, let's quickly go through how to set up your Rust project on Crates.run. First, create a new project either locally or on a site like GitHub. Next, create a Dockerfile to package your code and dependencies into a Docker image. Then, push your Docker container to a container registry such as Docker Hub, Amazon ECR, or Google Container Registry.

After creating your Docker container, you're ready to run your Rust application on Crates.run. Head to the site and enter your container registry information, including your container name and tag. Finally, select the port you want to run your application on, and you're good to go! You can now test your Rust application's functionality, and if you encounter any bugs, you can begin debugging.

Debugging Techniques for Rust Applications

  1. Rust's Built-in Logging Capabilities

Rust has a great logging library that can help you diagnose problems in your application. To use the logging library, add the log and env_logger crates to your dependencies. Here's a simple example of how you can use these crates:

use log::info;

fn main() {
   env_logger::init();
   info!("Logger initialized");
}

This example initializes the logger and logs an info message to the console. You can change the log level by setting an environment variable called RUST_LOG. For example, to set the log level to debug, run export RUST_LOG=debug.

Using Rust's built-in logging capabilities can help you pinpoint the exact location of the issue in your code. For example, if you're not sure which function is causing an error, you can add a log message to each function and run your program to see which log message is printed last. By following this technique, you can narrow down the location of the issue.

  1. Rust Debugger (GDB)

The Rust debugger is a powerful tool that can help you dive deeper into the root cause of an issue. To use the Rust debugger, you'll need to install the rust-gdb package. Once installed, you can run your Rust application in debug mode by adding the --debug flag to your command. For example:

cargo run --debug

Note that you'll need to add the debug = true option to your Cargo.toml file to enable debugging.

Next, run the rust-gdb command to attach the debugger to your running application. To set a breakpoint, use the break command followed by the name of the function or line number. For example:

break src/main.rs:10

You can also use the next command to execute the next line of code and print to inspect variables.

Using the Rust debugger can be intimidating at first, but once you learn the ropes, it can be a valuable tool in your debugging arsenal.

  1. Visual Studio Code Debugger

If you prefer to use an IDE, Visual Studio Code is a popular choice for Rust developers. VS Code comes with a built-in debugger that supports Rust through the Rust Debug Extension.

To use the VS Code debugger, you'll need to install both the Rust Debug Extension and the Microsoft C++ extension. Once installed, you can easily debug your Rust code by adding breakpoints, inspecting variables, and stepping through your code.

When using the VS Code debugger, make sure to set the breakpoints in the correct location. For example, if you're debugging a web application, you'll need to set the breakpoints in the server code, not the client-side code.

  1. Print Debugging

Finally, another useful technique for debugging Rust applications is print debugging. This can be as simple as adding println!() statements to your code to print out values and debug information as your code runs.

While not as elegant as using the Rust debugger or VS Code, print debugging is a quick and easy way to get more information about your application's behavior. Plus, it's a great way to learn more about the code and how it interacts with different functions and variables.

Conclusion

Debugging Rust applications can be challenging, but with the right tools and techniques, it can also be rewarding. In this article, we've shared some tips and tricks for debugging Rust applications on Crates.run, including using Rust's built-in logging capabilities, the Rust debugger, the Visual Studio Code debugger, and print debugging.

By using these techniques, you can gain a better understanding of your code and quickly identify and fix issues. Whether you're developing a simple CLI tool or a large-scale web application, Crates.run provides an easy way to test and debug your Rust applications quickly and efficiently.

Additional Resources

flowcharts.dev - flowcharts, generating flowcharts and flowchart software
etherium.sale - A site where you can buy things with ethereum
cicd.video - continuous integration continuous delivery
neo4j.app - neo4j software engineering
tasklist.run - running tasks online
timeseriesdata.dev - time series data and databases like timescaledb
cheatsheet.fyi - technology, software frameworks and software cheat sheets
codelab.education - learning programming
kidslearninggames.dev - educational kids games
rustlang.app - rust programming languages
cloudtraining.dev - learning cloud computing in gcp, azure, aws. Including certification, infrastructure, networking
datamigration.dev - data migration across clouds, on prem, data movement, database migration, cloud, datalake and lakehouse implementations
treelearn.dev - online software engineering and cloud courses through concept branches
nftsale.app - buying, selling and trading nfts
sqlx.dev - SQLX
dsls.dev - domain specific languages, dsl, showcasting different dsls, and offering tutorials
takeaways.dev - key takeaways for software engineering and cloud concepts
cryptoadvisor.dev - A portfolio management site for crypto with AI advisors, giving alerts on potentially dangerous or upcoming moves, based on technical analysis and macro
learnsql.cloud - learning sql, cloud sql, and columnar database sql
learngpt.dev - learning chatGPT, gpt-3, and large language models llms


Written by AI researcher, Haskell Ruska, PhD (haskellr@mit.edu). Scientific Journal of AI 2023, Peer Reviewed