Modules and crates - Rust for C-Programmers
Modules encapsulate Rust source code, hiding internal implementation details. Crates are the fundamental units of code compilation and distribution in Rust. Modules provide namespaces and control the visibility of items (functions, structs, etc.). Items within a module are private by default and must be explicitly marked pub (public) to be accessible from outside the module. For larger projects, a module’s contents can be placed in a separate file instead of directly within its parent file. When you declare a module using mod my_module; in a file (e.g., main.rs or lib.rs), the compiler looks for the module’s code in one of two locations: Cargo handles the process of finding and compiling these files automatically based on the mod declarations. A crate is the smallest unit of compilation and distribution in Rust. There are two types: A Cargo project (package) can contain one library crate and/or multiple binary crates.
Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help 2.9 Modules and Crates: Code Organization Modules encapsulate Rust source code, hiding internal implementation details. Crates are the fundamental units of code compilation and distribution in Rust. 2.9.1 Modules (mod) Modules provide namespaces and control the visibility of items (functions, structs, etc.). Items within a module are private by default and must be explicitly marked pub (public) to be accessible from outside the module.…
saved by
related reading
- The program structure - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Rust for C programmers - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Memory safety and ownership - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- What makes Rust special - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Constants and statics - Rust for C-Programmersrust-for-c-programmers.com
- Why Rust - Rust for C-Programmersrust-for-c-programmers.com
- Moves, cloning, and copying - Rust for C-Programmersrust-for-c-programmers.com