Cargo test vs cargo bench --tests


I can’t get cargo bench --tests passing while cargo test passes just fine.

cargo bench --tests
thread ‘main’ has overflowed its stack
fatal runtime error: stack overflow
error: An unknown error occurred

The code is basically as follows here:

    pub fn my_benchmark(b: &mut Bencher) {
      // setup test ...
      // ... 

      b.iter(|| {
      let result = tested_function();
      assert_eq!(result == true);

I’m following the recommendations from

I’d appreciate some pointer explaining why cargo bench and cargo test can behave differently here.


The main difference is that cargo bench compiles with optimizations. I think it will be identical if you run cargo test --release. But I don’t know why this would cause a stack overflow.


Figured this out: cargo bench executes multiple times not just the benchmarked closure but the whole method marked as #[bench]. It wasn’t obvious to me.

The setup code in the method had a bug when being called multiple times. That’s why things crash.