[SOLVED] Simple cargo question


#1

I know this is something simple that I am missing… but I can’t find out how to build a cargo project with more than one source file. An example is here: https://github.com/stusmall/mulitfile

Whenever I run “cargo build” it doesn’t build file.rs. What am I missing?

Thanks in advance!


#2

Because Cargo uses lib.rs as entry point by default and lib.rs doesn’t tell Rust about any other file. You need to use modules for that:

mod file;
use file::check;

fn it_works() {
  check();
}

The mod file; line tells Rust there is a module file.rs or file/mod.rs and the use file::check; line tells rust to import the check function from the “file” module so that you don’t have to call it using full module “path” file::check()

You can read more about modules in the book

Edit: Doing some checks I noticed I forgot to talk about visibility. For your check function to be callable from another module you need to annotate it with the pub keyword, essentially saying this item is accessible from outside the module.


#3

Perfect! Thank you so much!