Our cli have two functionalities (greetings and chifoumi)
It is always better to split logic in different files
- use modules
Modules and use Let you control the organization, scope, and privacy of paths
Let split our code in differents files
|-src
|- main.rs
|- greetings.rs
// greetings.rs
pub fn greets(name: &str) {
println!("Hello, {} 🦀 !", name);
}
Nb: functions are private by defaut, you must add pub
keyword
// main.rs
use std::env;
mod greetings; //declare module
use greetings::greets; // import function
fn main() {
let args: Vec<String> = env::args().collect();
let name = args.get(1).expect("Name is required");
greets(&name);
}
📌 Remember
- declare module and declare usage
- visibibility is private by default
📚 More resources
Expected tree
|-src
|- main.rs
|- greetings.rs
|- chifoumi.rs
💡 Tips
- do not forget visiblity
you understand how to organize code in modules
Check a solution with unit tests here
What you have learned
- modules
- visibility