1

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.

Tiina
  • 4,285
  • 7
  • 44
  • 73
  • 1
    Is this project a library or an executable? Technically, it _can_ be both, but this isn't usually what newcomers to Rust are intending when they add both a `lib.rs` and a `main.rs`.. – Peter Hall Apr 01 '21 at 10:47
  • @PeterHall both. I was studying Diesel demo. It has 1 binary, and 3 for library. But it was for Rust 2015. I was trying to change it to Rust 2018, and stuck at the path >.<. Then I started to study path, and got confused at `mod a; mod b` in `lib.rs` – Tiina Apr 02 '21 at 00:58

0 Answers0