A very basic question about crates


I am starting a small project and crated a crate with the cargo. In this I have 3 mod files say foo1.rs, foo2.rs , foo3.rs and a test.rs file. The crates name is first_project. I have to pub mod these files all in a lib.rs file for everything to compile properly, I have found out about that through some hardship. But If I dont add ,say , use crate::foo1::{fun1,fun2} to my test.rs file, it won’t see them inside the crate at all. Am I doing something wrong, I can’t seem to find an explanation of this.

Sorry if the question looks too newbie-like, I started learning Rust just a few days ago…

Thanks for the help!


No need to apologise.

It sounds like you’re describing files like this:


pub fn fun1() { ... }
pub fn fun2() { ... }


// contents are not relevant


// contents are not relevant


pub mod foo1;
pub mod foo2;
pub mod foo3;


// It sounds like you're asking why the below line is needed?
use crate::foo1::{fun1, fun2};

fn test() {

In order to just call fun1 and fun2 like that, they need to be imported into the test module. If you don’t want to import them into the test module, you instead have to fully specify their path. This should compile, for example:

fn test() {

Otherwise, how would it tell whether you wanted to call crate::foo1::func1, or wanted to call crate::foo2::func1?


Thank you for the answer! Hmm true that makes sense. I realized that it was needed by following the compiler’s directives ( tbh it is much more helpful than gcc or g++) but what I was curious why;
use foo1::{fun1,fun2};
wasn’t working. Also am I supposed to declare the testing module in lib.rs too ?


You can write tests like either:

// foo1.rs
pub fn fun1() { ... }

mod foo1_tests {
    pub fn fun1_test() {
        // Test fun1 or anything else in here

and not need to import anything or you can write them like so:

// test.rs
use crate::*;
pub mod tests_for_library {
    pub fn test_foo1() {
        //use foo1's fns here

The reasoning behind needing to import your own crate is that the test.rs file is treated like a separate crate/library (but not really because it has access to the crate namespace) and it depends on your main library.