Skip to content
Justice Tech School

Tests and Docs

#[test] marks tests run by cargo test. /// writes documentation comments.

Example
/// Adds two numbers.
fn add(a: i32, b: i32) -> i32 { a + b }

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn adds() { assert_eq!(add(2, 3), 5); }
}

fn main() {
    println!("{}", add(2, 3));
}

Result

5

Exercise

Test attribute.

#[]

Quiz Test

Which command runs tests?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.