As a first project in Rust, I'm trying to create a run length encoding library.
This is my src/lib.rs (initially created by cargo):
#[cfg(test)]
mod rle;
mod tests {
#[test]
fn it_works() {
let rle1 = rle::Rle {
lengths: vec![1, 4, 2],
values: vec![1, 2, 1],
};
}
}
This is my src/rle.rs:
pub struct Rle {
lengths: Vec<i32>,
values: Vec<i32>,
}
Using this layout, I get an error[E0433]: failed to resolve. Use of undeclared type or module 'rle'
I have tried to follow the guidelines in the Rust Docs called Crates and Modules.
What am I doing wrong?
In case the layout was not obvious:
$ ls src
lib.rs rle.rs
You have an error in the use of #[cfg(test)].
Rust has two ways of applying attributes:
#[...] applies to the next item#![...] applies to the enclosing itemThis means that here, #[cfg(test)] is applying to the next item (mod rle;) which will thus only be compiled in test mode. On the other hand, your tests module will always be compiled.
As a result, when NOT running in test mode, you have:
rle moduletests module referring to a rle moduleThe fix is simple: move the attribute so it applies to mod tests, not mod rle.
Note:
super::rle to refer to the rle module.Rle public to be able to name them from outside the file (a problem you should not have if the tests are in the same file).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With