Code organization
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.
Functions
Modules
Can be mapped to a,
Inline module
File
Directory hierarchy
Crates
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
Workspaces
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 printing the String inside the function.
Last updated