How can I use a module from outside the src folder

2020-02-06 06:57发布

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.

标签: rust
1条回答
贪生不怕死
2楼-- · 2020-02-06 07:45

Here's a literal answer, but don't actually use this!

#![feature(test)]
extern crate test;

#[path = "../src/foo.rs"] // Here
mod foo;

#[bench]
fn bencher(_: &mut test::Bencher) {
    println!("{:?}", foo::Thang);
}

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:

demo
├── Cargo.lock
├── Cargo.toml
├── benches
│   └── crypto_bench.rs
├── benchmarks
└── src
    ├── bin
    │   └── main.rs
    ├── crypto.rs
    └── lib.rs

Move the reusable code to a library:

src/lib.rs

pub mod crypto;

src/crypto.rs

pub struct Crypto;
impl Crypto {
    pub fn secret() {}
}

Then import your library from the benchmark and the binary:

benches/crypto_bench.rs

#![feature(test)]

extern crate test;

use demo::crypto::Crypto;
use test::Bencher;

#[bench]
fn speedy(b: &mut Bencher) {
    b.iter(|| Crypto::secret());
}

src/bin/main.rs

use demo::crypto::Crypto;

fn main() {
    Crypto::secret();
    eprintln!("Did the secret thing!");
}

You can then run it in different ways:

$ cargo build
   Compiling demo v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.51s

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/main`
Did the secret thing!

$ cargo +nightly bench
   Compiling demo v0.1.0 (/private/tmp/example)
    Finished release [optimized] target(s) in 0.70s
     Running target/release/deps/my_benchmark-5c9c5716763252a0

running 1 test
test speedy ... bench:           1 ns/iter (+/- 0)

test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out

See also:

查看更多
登录 后发表回答