What is a “Package” in Rust?
A Cargo feature that lets you build, test, and share crates. It can contain multiple binary crates and optionally one library crate.
What is a “Crate” in Rust?
A tree of modules that produces either a library or an executable.
What is a “Path” in the context of Rust code organization?
A way of naming a specific item, such as a struct, function, or module, so the compiler can locate it.
What defines a “Binary Crate”?
It is a crate that compiles into an executable program and must contain a main function.
What defines a “Library Crate”?
It is a crate that does not have a main function and does not compile to an executable; it defines shared functionality for other projects to use.
What is the maximum number of library crates a single package can contain?
A package can contain at most one library crate.
How many binary crates can a single package contain?
A package can contain as many binary crates as you like.
If a package contains src/main.rs, what does Cargo assume?
Cargo assumes it is the crate root of a binary crate having the same name as the package.
If a package contains src/lib.rs, what does Cargo assume?
Cargo assumes it is the crate root of a library crate having the same name as the package.
Where should you place source files if you want a package to contain multiple binary crates?
You should place them in the src/bin directory; each file there becomes a separate binary crate.
When compiling a crate, where does the compiler look for code first?
It starts at the crate root file (usually src/lib.rs for libraries or src/main.rs for binaries).
If you declare mod garden; in the crate root, where does the compiler look for the code?
If you declare mod vegetables; inside src/garden.rs, where does the compiler look for the submodule’s code?
What is the “module tree”?
The hierarchical structure of modules in a crate, starting from the implicit module named crate at the root, similar to a filesystem directory tree.
What are the two forms of paths you can use to refer to an item in a Rust module tree?
How does an absolute path begin when referring to code within the current crate?
It begins with the literal crate.
How does a relative path begin?
It begins with self, super, or an identifier in the current module.
What is the default privacy setting for items (functions, structs, enums, modules) in Rust?
They are private to their parent modules by default.
What does the super keyword allow you to do in a path?
It allows you to start a relative path from the parent module (similar to .. in a filesystem).
If you make an enum public with pub, are its variants automatically public?
Yes, if an enum is public, all of its variants are automatically public.
Why is it beneficial to move most logic into the library crate rather than the binary crate?
It allows external projects to share and reuse the functionality, whereas code locked inside a binary crate cannot be easily reused by others.
In a package with both binary and library crates, where should the module tree be defined?
It should be defined in src/lib.rs.
How does the binary crate access code from the library crate within the same package?
It accesses the library using the package name (starting paths with the package name), treating the library exactly like an external crate.
How does separating the binary and library help with API design?
It forces you to act as a client of your own code, ensuring the public interface is well-designed and usable before other users try it.