0
0
Rustprogramming~10 mins

Defining modules in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a module named math.

Rust
mod [1] {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}
Drag options to blanks, or click blank then click option'
Acalc
Badd
Cmath
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name instead of a module name.
Using a name that doesn't match the module's purpose.
2fill in blank
medium

Complete the code to make the function greet accessible outside the module.

Rust
mod greetings {
    [1] greet() {
        println!("Hello!");
    }
}
Drag options to blanks, or click blank then click option'
Apub fn
Blet
Cfn
Dmod
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add pub to the function.
Using let or mod incorrectly.
3fill in blank
hard

Fix the error in the code to correctly call the hello function from the salutations module.

Rust
pub mod salutations {
    pub fn hello() {
        println!("Hi!");
    }
}

fn main() {
    salutations::[1]();
}
Drag options to blanks, or click blank then click option'
Ahello
Bgreet
Chi
Dsay_hello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name that does not exist.
Forgetting the double colon :: syntax.
4fill in blank
hard

Fill both blanks to define a nested module utils inside helpers and make the function print_msg public.

Rust
mod helpers {
    pub mod [1] {
        pub fn [2]() {
            println!("Message");
        }
    }
}
Drag options to blanks, or click blank then click option'
Autils
Bprint_msg
Cmessage
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module or function names.
Forgetting to make the module or function public.
5fill in blank
hard

Fill all three blanks to create a module config with a public constant VERSION and a public function show_version that prints it.

Rust
mod [1] {
    pub const [2]: &str = "1.0";
    pub fn [3]() {
        println!("Version: {}", [2]);
    }
}
Drag options to blanks, or click blank then click option'
Aconfig
BVERSION
Cshow_version
Dversion
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase for constant names.
Forgetting to make the constant or function public.
Mismatching names between constant and usage.