Rust Modules and Crates: Project Organization Quiz Quiz

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.

  1. Identifying a Module

    In Rust, how do you define a module called 'network' in the main.rs file?

    1. use network;
    2. mod network;
    3. crate network;
    4. fn network();

    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.

  2. Default Visibility of Modules

    What is the default visibility of items inside a Rust module if no visibility keyword is specified?

    1. protected
    2. private
    3. visible to all crates
    4. public

    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.

  3. Separate File Modules

    Which file should exist for a module declared as 'mod math;' in your main.rs?

    1. math.rsx
    2. math.mod
    3. math.rs
    4. math.rc

    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.

  4. Crate Root Identification

    In a Rust binary project, which file serves as the crate root by default?

    1. src/lib.rs
    2. Cargo.toml
    3. src/main.rs
    4. main.rs in the root folder

    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.

  5. Visibility with 'pub' Keyword

    What does adding the 'pub' keyword before a module or function declaration in Rust do?

    1. Defines a new crate
    2. Imports it into the current scope
    3. Makes it accessible from outside its module
    4. Changes its type to public

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

  6. Bringing a Module into Scope

    If you have 'mod utils;' and want to use 'utils::helper()', what statement must appear in your code?

    1. open utils.helper;
    2. require utils.helper;
    3. import utils:helper;
    4. use utils::helper;

    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.

  7. Nested Module File Organization

    If you declare 'mod server' in main.rs and then 'mod api' inside server.rs, where should you place the 'api' module?

    1. Directly in Cargo.toml
    2. In a file named main/api.rs
    3. In a file named server/api.rs
    4. In the top-level api.rs

    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.

  8. Purpose of Cargo.toml

    What is the main purpose of the Cargo.toml file in a Rust project?

    1. Managing dependencies and project metadata
    2. Setting compiler optimization flags only
    3. Storing module source code
    4. Defining function signatures

    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.

  9. Crate Types in Rust

    Which of the following is NOT a valid crate type in Rust?

    1. Binary Crate
    2. Proc-macro Crate
    3. Library Crate
    4. script Crate

    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.

  10. Accessing a Struct from Another Module

    If a struct 'User' is defined as 'pub struct User' in 'models.rs', what must you do to access it in 'main.rs'?

    1. Prefix with 'mod' in main.rs
    2. Use 'use crate::models::User;' in main.rs
    3. Call it directly without any import
    4. Declare the struct again 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.