Moving Code from lib.rs to another file causes Never Used Warnings

I have created a library crate and all code is in lib.rs. Now lib.rs is getting to be too large, so I want to move code out to other files (not other folders). But when I do, the other files will not compile because the structs, methods, and so forth in those files are marked as unused. Here is the simplest example I could come up with to illustrate this:

lib.rs:

pub struct S1 {}

impl S1 {
pub fn new() -> S1 {
S1 {}
}
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

This builds and the test runs.
Now I move the S1 related code to s1.rs in the same folder:

s1.rs:

pub struct S1 {}

impl S1 {
pub fn new() -> S1 {
S1 {}
}
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

and lib.rs:

mod s1;

Now if I try to build, the compiler complains that 'S1' is never constructed, and 'new' is never used. The test still executes, however.

Is there something I can do, other than turning off the dead_code warning, that will allow me to compile s1.rs?

When something is pub in lib.rs, it can be accessed from outside, so it's assumed to be used externally.

When something is pub in a private module, it can only be accessed from within the crate, not from outisde (privacy of the module acts like a firewall). Then the compiler sees the crate doesn't use it.

kornel, thanks for the reply that explains why it doesn't compile. It only took me about 45 minutes of playing to determine that the minimal solution appears to be to change the code in lib.rs to be:

pub mod s1;

Of course, I may be wrong as to the full solution, but at least that has gotten past that hurdle.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.