Rust Import Module

Suppose we have two files src/main.rs and src/utils.rs, to use utils.rs in main.rs, there are two ways that work.

Use mod keyword in main.rs

1
2
3
// main.rs
mod crate::utils;
// mod utils; // use relative path to import

Use pub mod keyword in lib.rs

create a lib.rs file in src directory.

1
2
// lib.rs
pub mod utils;

then add this in main.rs

1
use <packageName>::utils;