Rust
Version
Running on Rust v1.79
Supported languages
Rust
Testing framework
Special reminders and implementation details
#[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
Reading input (stdin)
Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.
use std::io;
fn main() {
let mut name = String::new();
io::stdin().read_line(&mut name).expect("failed to read the name");
println!("Hello, {}!", name.trim());
}read_line appends the line including its newline, so trim it; at end-of-file it appends nothing and returns Ok(0).
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
Identify the Problem Area: Locate the section of your code where you suspect the problem might be.
Insert
log::info!Statements: Addlog::info!statements before and after the suspected problematic code to print out variable values and flow control.Check the Results Output of your tests
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:
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
}