Import crates and modules

I have a very primitive question.
In the book is written

  • An absolute path starts from a crate root by using a crate name or a literal crate .

So I'm confused about this.

I have
--crates_test
-----src
------------lib.rs
------------main.rs
------------foo.rs
------------bar.rs

Now
lib.rs

pub mod foo;
pub mod bar;

main.rs

extern crate crates_test;

use crates_test::{bar,foo};

fn main(){
    bar::func_bar();
    foo::func_foo();
}

foo.rs

pub fn func_foo(){
    println!("{}", "this is foo.rs");
}

Now coming to the bar
bar.rs

pub fn func_bar(){
    crate::foo::func_foo();
    println!("{}", "this is bar.rs");
}

This works. Also if I change bar.rs to

use crate::foo;

pub fn func_bar(){
  foo::func_foo();
    println!("{}", "this is bar.rs");
}

This also works. However if I change to

use crates_test::foo;

pub fn func_bar(){
  foo::func_foo();
    println!("{}", "this is bar.rs");
}

I get

error[E0432]: unresolved import `crates_test`
 --> src\bar.rs:1:5
  |
1 | use crates_test::foo;
  |     ^^^^^^^^^^^ use of undeclared type or module `crates_test`

error: aborting due to previous error

Also I can not use crates_test::foo::func_foo();

How is it related to what is written in the book?

However, in the main it works as can be seen use crates_test::{bar,foo};. I guess it works due to extern crate. But I confused anyway.

In all examples in the book it is used either self, super or crate.

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