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.

  1. Functions

  2. Modules

    Can be mapped to a,

    • Inline module

    • File

    • Directory hierarchy

  3. 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

  4. 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 the String from the function and print it when calling it, instead printing the String inside the function.

Last updated