Problem with unresolved import

I have a small educational project on Rust.
I decided to add performance measurements and ran into a problem - error[E0432]: unresolved import.
Project tree:

src\                                                 
   main.rs
src\benches\                                         
   mod.rs
   benchmarks.rs
src\easy\                                            
   mod.rs
   task_00001.rs

main.rs:

extern crate criterion;
pub mod easy;
pub mod benches;

struct Solution;

fn main() {
}

src\benches\mod.rs:

pub mod benchmarks;

src\benches\benchmarks.rs:

use crate::Solution;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("count_largest_group 9999", |b| b.iter(|| Solution::count_largest_group(black_box(9999))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

And after cargo bench i have error:

error[E0432]: unresolved import `crate::Solution`
 --> src/benches/benchmarks.rs:1:5
  |
1 | use crate::Solution;
  |     ^^^^^^^^^^^^^^^ no `Solution` in the root

Why this code working in src\easy\task_00001.rs, but didn't working here?

You need to rename main.rs to lib.rs. Then, instead of crate::, you should use your_package_name_goes_here::.

1 Like

Thank's for your answer! It's work!
Additionally, I changed the line in lib.rs => pub struct Solution and move folder benches out from src

You usually wouldn't have benchmarks inside src/. Try moving benches so it's next to src/. You would typically also use snake case for package names.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.