Complete the code to declare a module named math.
mod [1] {}fn instead of a module name.use instead of mod.The keyword mod declares a module, and math is the module name here.
Complete the code to use a function add from the math module.
fn main() {
let sum = math::[1](2, 3);
println!("Sum: {}", sum);
}To call a function inside a module, use module_name::function_name. Here, add is the function.
Fix the error by completing the code to declare a public function add inside the math module.
mod math {
pub fn [1](a: i32, b: i32) -> i32 {
a + b
}
}pub to make the function accessible.The function name must be add to match the usage. pub makes it accessible outside the module.
Fill both blanks to import the add function from the math module and use it.
use crate::math::[1]; fn main() { let result = [2](5, 7); println!("Result: {}", result); }
We import add with use and then call it directly by name.
Fill all three blanks to create a module utils with a public function greet and call it in main.
mod [1] { pub fn [2]() { println!("Hello from utils!"); } } fn main() { [3](); }
The module is named utils, the function is greet, and we call greet() in main.