0
0
Rustprogramming~10 mins

Why modules are used in Rust - Test Your Understanding

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] {}
Drag options to blanks, or click blank then click option'
Afn
Bmain
Cmath
Duse
Attempts:
3 left
💡 Hint
Common Mistakes
Using fn instead of a module name.
Writing use instead of mod.
2fill in blank
medium

Complete the code to use a function add from the math module.

Rust
fn main() {
    let sum = math::[1](2, 3);
    println!("Sum: {}", sum);
}
Drag options to blanks, or click blank then click option'
Aadd
Bsubtract
Cmod
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Using the module name as a function.
Calling a function that does not exist.
3fill in blank
hard

Fix the error by completing the code to declare a public function add inside the math module.

Rust
mod math {
    pub fn [1](a: i32, b: i32) -> i32 {
        a + b
    }
}
Drag options to blanks, or click blank then click option'
Aadd
Bsum
Cmain
Dmod
Attempts:
3 left
💡 Hint
Common Mistakes
Not using pub to make the function accessible.
Using a different function name than called.
4fill in blank
hard

Fill both blanks to import the add function from the math module and use it.

Rust
use crate::math::[1];

fn main() {
    let result = [2](5, 7);
    println!("Result: {}", result);
}
Drag options to blanks, or click blank then click option'
Aadd
Bsubtract
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for import and call.
Not importing the function before use.
5fill in blank
hard

Fill all three blanks to create a module utils with a public function greet and call it in main.

Rust
mod [1] {
    pub fn [2]() {
        println!("Hello from utils!");
    }
}

fn main() {
    [3]();
}
Drag options to blanks, or click blank then click option'
Autils
Bgreet
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Not making the function public.
Calling the function without the correct name.