Reputation: 32008
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
Upvotes: 7
Views: 8682
Reputation: 300349
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).Upvotes: 7
Reputation: 22273
You first need to make Rle
fields public to be able to access them:
pub struct Rle {
pub lengths: Vec<i32>,
pub values: Vec<i32>,
}
And then, in the lib.rs
file you should use it like so:
mod rle; // you probably don't want #[cfg(test)] to apply to rle
#[cfg(test)] // this makes the tests module run only during cargo test
mod tests {
use super::rle::Rle; // now Rle is applicable in the tests module
#[test]
fn it_works() {
let rle1 = Rle {
lengths: vec![1, 4, 2],
values: vec![1, 2, 1],
};
}
}
Note that I changed let rle1 = rle::Rle {
to let rle1 = Rle {
, because I imported super::rle::Rle
and not just super::rle
.
This compiles without issues.
Upvotes: 5