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...
// It sounds like you're asking why the below line is needed?
use crate::foo1::{fun1, fun2};
fn test() {
fun1();
fun2();
}
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:
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 ?
// foo1.rs
pub fn fun1() { ... }
#[cfg(test)]
mod foo1_tests {
#[test]
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::*;
#[cfg(test)]
pub mod tests_for_library {
#[test]
pub fn test_foo1() {
//use foo1's fns here
foo1::fun1();
}
}
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.