Thread has overflowed its stack

โš“ Rust    ๐Ÿ“… 2026-05-22    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 2      

surdeus

Hello there! I'm new to Rust and now started to solve "100 exercise to learn Rust"(GitHub - mainmatter/100-exercises-to-learn-rust: A self-paced course to learn Rust, one exercise at a time. ยท GitHub)

So there is the code:


fn factorial(n: u32) -> u32 {
    if n < 2 {
        1
    } else {
        n * factorial(n) - 1
    }
}

#[cfg(test)]
mod tests {
    use crate::factorial;
    
    #[test]
    fn first() {
        assert_eq!(factorial(0), 1);
    }
    
    #[test]
    fn second() {
        assert_eq!(factorial(1), 1);
    }
    
    #[test]
    fn third() {
        assert_eq!(factorial(2), 2);
    }
    
    #[test]
    fn fifth() {
        assert_eq!(factorial(5), 120);
    }
}

When I try to run ''cargo test" I have this message:

running 4 tests
test tests::first ... ok

thread 'tests::second' (308539) has overflowed its stack
fatal runtime error: stack overflow, aborting
error: test failed, to rerun pass `--lib`

Caused by:
  process didn't exit successfully: `/home/.../Rust/100exercises/2/factorial/target/debug/deps/factorial-23c7f241ea6c1944` (signal: 6, SIGABRT: process abort signal)

Does anybody know why is it don't exit successfully?

6 posts - 4 participants

Read full topic

๐Ÿท๏ธ Rust_feed