Explore core Rust concepts with these questions on modules, crates, and project organization. This quiz targets beginners looking to understand file structure, visibility rules, and best practices for organizing Rust code.
In Rust, how do you define a module called 'network' in the main.rs file?
Explanation: In Rust, 'mod network;' declares a module named 'network' in the main.rs file, allowing you to organize code further. 'use network;' is for importing, not defining. 'crate network;' is incorrect syntax, and 'fn network();' defines a function, not a module.
What is the default visibility of items inside a Rust module if no visibility keyword is specified?
Explanation: By default, items inside a Rust module are private, meaning they are only accessible within the same module or its submodules. 'public' would require the pub keyword. 'protected' is not used in Rust, and 'visible to all crates' is another way to say public, which is incorrect by default.
Which file should exist for a module declared as 'mod math;' in your main.rs?
Explanation: When you write 'mod math;' in main.rs, Rust expects a file named math.rs or a directory named math with a mod.rs inside it. 'math.mod', 'math.rsx', and 'math.rc' are invalid and not recognized as valid Rust source files for modules.
In a Rust binary project, which file serves as the crate root by default?
Explanation: For binary projects, src/main.rs serves as the crate root, which is the entry point of the program. src/lib.rs is used as the crate root for library projects. Cargo.toml is a configuration file, and placing main.rs in the root directory does not make it the crate root.
What does adding the 'pub' keyword before a module or function declaration in Rust do?
Explanation: The 'pub' keyword makes the module or function accessible from outside its defining module, allowing other modules or crates to use it. It does not change its type, define a crate, or import it. 'Imports it into the current scope' is achieved using 'use', not 'pub'.
If you have 'mod utils;' and want to use 'utils::helper()', what statement must appear in your code?
Explanation: To access 'helper' from 'utils', you use 'use utils::helper;'. The other options are not valid Rust syntax. 'import' and 'require' do not exist in Rust, and 'open' is also not a recognized Rust keyword.
If you declare 'mod server' in main.rs and then 'mod api' inside server.rs, where should you place the 'api' module?
Explanation: Nested modules like 'api' declared within 'server.rs' are typically placed in server/api.rs for organized project structure. main/api.rs and a top-level api.rs would not match the module hierarchy. Cargo.toml is unrelated to source code storage.
What is the main purpose of the Cargo.toml file in a Rust project?
Explanation: Cargo.toml is mainly used to manage dependencies, versioning, and metadata for a Rust project. Module source code is stored in .rs files. Function signatures and compiler flags are not its primary focus; compiler settings go elsewhere if needed.
Which of the following is NOT a valid crate type in Rust?
Explanation: Rust does not have a 'script Crate' type. The valid crate types are Binary Crate, Library Crate, and Proc-macro Crate. 'script Crate' is not recognized and does not exist in the language.
If a struct 'User' is defined as 'pub struct User' in 'models.rs', what must you do to access it in 'main.rs'?
Explanation: To access 'User' in main.rs, you must bring it into scope with 'use crate::models::User;'. Direct calls are not possible without use, and redeclaring creates a new struct. Prefixing with 'mod' only declares the module, not the struct.