A crate root is the primary source file that the Rust compiler (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
rustc) uses as the entry point to compile a crate and construct its module tree. It serves as the root node of the crate’s namespace hierarchy, implicitly defining a top-level module named crate.
Standard File Conventions
By default, the Cargo build system looks for specific file paths to identify crate roots:- Binary Crate:
src/main.rs - Library Crate:
src/lib.rs
src/main.rs and src/lib.rs, it possesses two distinct crate roots, resulting in two separate crates (one binary, one library) compiled from the same package. Additional binary crate roots can be defined by placing files in the src/bin/ directory.
Module Tree Construction
The compiler only processes files that are explicitly included in the module tree. The crate root is responsible for declaring top-level child modules using themod keyword. When the compiler encounters a mod declaration in the crate root, it searches the file system for the corresponding module implementation.
Path Resolution
Because the crate root establishes the base of the module hierarchy, absolute paths within the crate always begin with thecrate:: prefix, which explicitly references the crate root.
Compilation Unit Boundary
The crate root defines the strict boundary of the compilation unit. Any.rs file in the project directory that is not declared via a mod statement originating from the crate root—either directly or transitively through child modules—is entirely ignored by the compiler. The compiler parses the crate root first, follows the mod declarations to other files, and aggregates all reachable modules into a single Abstract Syntax Tree (AST) to produce the final binary or library artifact.
Master Rust with Deep Grasping Methodology!Learn More





