When a single code block is getting larger, it should be decomposed into smaller pieces and should be organized in a proper manner. Rust supports different levels of code organization.
Can be mapped to a,
- Inline module
- File
- Directory hierarchy
Can be mapped to a,
-
lib.rs file on the same executable crate
-
Dependency crate specified on Cargo.toml
Can be specified from,
- Path
- Git repository
- crates.io
Helps to manage multiple crates as a single project.
Let’s discuss one by one.
💡 To make examples more simpler, we use a simple function which prints
“Hello, world!”
. But regarding writing testable codes, always try to return theString
from the function and print it when calling it, instead of printing theString
inside the function.