Skip to content

Rust

Version

Running on Rust v1.79

Supported languages

Rust

Testing framework

Builtin test

Special reminders and implementation details

rs
#[cfg(test)]
use crate::HelloWorld::hello_world;

mod nested_tests {
    use super::*;
    #[test]
    fn test_hello_world_not_empty() {
        assert!(!hello_world().is_empty());
    }

    #[test]
    fn test_hello_world_contains_hello() {
        assert!(hello_world().contains("Hello"));
    }

    #[test]
    fn test_hello_world_exact_length() {
        assert_eq!(hello_world().len(), 13);
    }
}

Included libraries

How to debug

In Rust, you can use the log crate to print debug information. This is flexible and powerful because you can control the log level and format.

Steps to Debug Using log::info! Statements

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert log::info! Statements: Add log::info! statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here’s an example of how to use log::info! for debugging:

rs
pub fn sum(a: i32, b: i32) -> i32 {
    log::info!("Function called with arguments: {}, {}", a, b); // Log function call
    let result = a + b;
    log::info!("Sum after addition: {}", result); // Log sum calculation
    result
}