In many posts, as in sibling dependency people suggest to use mod xxx
or even pub mod xxx
in lib.rs
when they are at the same level under root, I tried it too.
src/lib.rs
:
pub mod printer;
pub mod screen;
src/printer.rs
:
pub fn says(a: &str) {
println!("printer says {}",a);
}
src/screen.rs
:
use crate::printer;
pub fn says(b: &str) {
printer::says("hi");
println!("screen says {}", b);
}
src/main.rs
mod printer;
mod screen;
fn main() {
printer::says("hi");
screen::says("hi");
}
The above worked, until I tried different lib.rs
and I found that, when lib.rs
is
pub mod screen;
the error is:
error[E0432]: unresolved import `crate::printer`
--> src\screen.rs:1:5
|
1 | use crate::printer;
| ^^^^^^^^^^^^^^ no `printer` in the root
Actually, lib.rs
can have both or neither mod screen and printer or only printer; if only it is not only screen.