Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The keyword
mod is used to declare a module. Here, the module is named math.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add
pub to the function.Using
let or mod incorrectly.✗ Incorrect
Adding
pub before fn makes the function public and accessible outside the module.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name that does not exist.
Forgetting the double colon
:: syntax.✗ Incorrect
To call the function, use the module name followed by :: and the function name exactly as declared, which is
hello.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module or function names.
Forgetting to make the module or function public.
✗ Incorrect
The nested module is named
utils and the function inside is print_msg, both made public.5fill in blank
hardFill 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'
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.
✗ Incorrect
The module is
config, the constant is VERSION, and the function is show_version which prints the version.