Complete these tasks to reinforce what you learned in this module.
Implement a function double(n: i32) -> i32 that returns n * 2. Call it from main with 5 and print the result.
// Task 1: double function. See tasks/TASKS.md.
fn main() {
// YOUR CODE HERE: define double(n: i32) -> i32, call double(5), print "double(5) = 10"
todo!("Implement double and call it");
}
Implement max(a: i32, b: i32) -> i32 that returns the greater of the two. Call it with 3 and 7 and print the result.
if expression that returns one of the two values, or use return in one branch.// Task 2: max function. See tasks/TASKS.md.
fn main() {
// YOUR CODE HERE: define max(a, b), call max(3, 7), print "max(3, 7) = 7"
todo!("Implement max and call it");
}
Implement fn max3(a: i32, b: i32, c: i32) -> i32 that returns the largest of the three values. This task is graded by hidden tests you can't see: they check all three positions, negative numbers, and ties — so a solution that only compares two of the three values will not pass. (This is how real graders prevent under-general answers; the public Playground runs the hidden cargo test for you.)
i32 has a .max() method, and you can chain it: a.max(b).max(c).// Task 3: implement max3, graded by HIDDEN tests (you won't see them). They check
// all three positions, negatives, and ties. See tasks/TASKS.md.
fn max3(a: i32, b: i32, c: i32) -> i32 {
todo!("return the largest of a, b, and c");
}
fn main() {
// `cargo test` ignores main; it's here so this file also builds as a binary.
println!("max3(1, 2, 3) = {}", max3(1, 2, 3));
}