Debug Rust code
You have created and run your Rust application. Let's imagine you have discovered that it functions not the way you expected. For example, it returns a wrong value or crashes with an exception. Seems like you have errors in your code, and it’s time to debug it.
What Is Debugging?
Broadly, debugging is the process of detecting and correcting errors in a program.
There are different kinds of errors, which you are going to deal with. Some of them are easy to catch, like syntax errors, because they are taken care of by the compiler. Another easy case is when the error can be quickly identified by looking at the stack trace, which helps you figure out where the error occurred.
However, there are errors that can be very tricky and take really long to find and fix. For example, a subtle logic error, which happened early in the program may not manifest itself until very late, and sometimes it is a real challenge to sort things out.
This is where the debugger is useful. The debugger is a powerful tool, which lets you find bugs a lot faster by providing an insight into the internal operations of a program. This is possible by pausing the execution and analyzing the state of the program by thorough examination of variables and how they are changed line by line. While debugging, you are in full control of the things. In this manual we are covering a basic debugging scenario to get you started.
Examine the code
Let's try a simple debugging case. Imagine we have the following application:
The program is supposed to calculate the average of all values passed as command-line arguments.
It compiles and runs without issues; however, the result is not what one would expect. For instance, when we pass
1 2 3 as the input, the result is
First of all, you need to think about where the suspected error might be coming from. We can assume the problem is not in the print statements. Most likely, unexpected results are coming from our
findAverage function. In order to find the cause, let's examine its behavior in the runtime.
To examine how the program operates at runtime, we need to suspend its execution before the suspected piece of code. This is done by setting breakpoints. Breakpoints indicate the lines of code where the program will be suspended for you to examine its state.
Click the gutter at the line where the
findAveragefunction is called.
Run the program in debug mode
Now let's start the program in debug mode.
Since we are going to pass arguments for running and debugging the program, make sure the run/debug configuration has these arguments in place.
Press ⌘ R, select the run configuration, and then press ⌥ ⏎.
Analyze the program state
After the debugger session has started, the program runs normally until a breakpoint is hit. When this happens, the line where the program paused gets highlighted and the Debug tool appears.
The highlighted line has not been executed yet. The program now waits for further instructions from you. The suspended state lets you examine variables, which hold the state of the program.
findAverage function has not been called yet, all its local variables like
result are not yet in scope, however, we can examine the contents of the
args array (
args is in scope for the
main function). The contents of
args are displayed in the Variables panel:
Step through the program
Now that we are comfortable with the Debug tool, it's time to step into the
findAverage function and find out what is happening inside it.
To step into a function, click the Step Into button or press ⌘ ;.
Another line gets highlighted in the editor because we advanced the execution point one step forward.
Continue stepping with Step Over ⌘ '. Notice how it is different from Step Into. While it also advances the execution one step forward, it doesn't visit other functions like
to_float()along the way.
Let's keep stepping and see how the local variable
resultis declared and how it is changed with each iteration of the loop.
Right now the variable contains the value
"3". It is going to be converted to f64 and be added to
result, which currently has the value of . No errors so far. The sum is calculated correctly.
Two more steps take us to the
returnstatement, and we see where the omission was. We are returning
result, which has the value of , without dividing it by the number of inputs. This was the cause of incorrect program output.
Let's correct the error:result / args.len() as f64
Stop the debugger session and rerun the program
In order to check that the program works fine, let's stop the debugger session and rerun the program.
Stopbutton or press ⌘ ⇧ \.
Press ⌘ R, select the run configuration, and launch it in the Run mode.
Verify that the program works correctly now.Compiling find_average v0.1.0 (/Users/jetbrains/my_directory/find_average) Finished dev [unoptimized + debuginfo] target(s) in 0.20s Running `target/debug/find_average 1 2 3` Average finder v.0.1 The average is 2