My project's path structure is as follows:
demo
├── benches
│ └── crypto_bench.rs
├── src
│ ├── main.rs
│ └── crypto.rs
├── Cargo.lock
└── Cargo.toml
crypto.rs
contains a struct Crypto
with implementation.
crypto.rs
is referred to from main.rs
using mod crypto;
How can I use crypto.rs
from crypto_bench.rs
inside the benches folder?
I have tried all kinds of variations of extern crate
, mod
, super
and use
.
All examples I could find online are for library projects with a lib.rs
and those "imports" don't work when using a project with a main.rs
file.
Here's a literal answer, but don't actually use this!
In fact, it's very likely that this won't work because your code in
foo.rs
needs supporting code from other files that won't be included.Instead of doing this, just create a library. You have the pure definition of a library - a piece of code that wants to be used in two different executables. You don't have to give up having an executable or even create separate directories (see Rust package with both a library and a binary?), but creating reusable code is a key component of making good code.
Your end state would look something like:
Move the reusable code to a library:
src/lib.rs
src/crypto.rs
Then import your library from the benchmark and the binary:
benches/crypto_bench.rs
src/bin/main.rs
You can then run it in different ways:
See also: