Challenge - 5 Problems
Rust Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this Rust code using arithmetic operators?
Consider the following Rust code snippet. What will it print when run?
Rust
fn main() {
let a = 10;
let b = 3;
let result = a % b + a / b * b;
println!("{}", result);
}Attempts:
2 left
๐ก Hint
Remember the order of operations: division and multiplication happen before addition.
โ Incorrect
The expression is evaluated as (a % b) + ((a / b) * b). Here, a % b is 10 % 3 = 1, a / b is 10 / 3 = 3 (integer division), so 3 * 3 = 9. Adding 1 + 9 gives 10.
โ Predict Output
intermediate2:00remaining
What is the value of x after this Rust code runs?
Look at this Rust code. What is the final value of x?
Rust
fn main() {
let mut x = 5;
x += 3 * 2 - 4 / 2;
println!("{}", x);
}Attempts:
2 left
๐ก Hint
Calculate the expression on the right side first, then add to x.
โ Incorrect
The expression is 3 * 2 - 4 / 2 = 6 - 2 = 4. Then x += 4 means x = 5 + 4 = 9.
โ Predict Output
advanced2:00remaining
What does this Rust code print?
Analyze this Rust code snippet and determine its output.
Rust
fn main() {
let a = 7;
let b = 2;
let c = a / b;
let d = a as f64 / b as f64;
println!("{} {}", c, d);
}Attempts:
2 left
๐ก Hint
Integer division truncates, but casting to float allows decimal division.
โ Incorrect
a / b is integer division: 7 / 2 = 3. Casting to f64 makes it floating point division: 7.0 / 2.0 = 3.5.
โ Predict Output
advanced2:00remaining
What is the output of this Rust code with mixed arithmetic?
What will this Rust program print?
Rust
fn main() {
let x = 4;
let y = 3;
let z = (x + y) * (x - y) / y;
println!("{}", z);
}Attempts:
2 left
๐ก Hint
Calculate inside parentheses first, then multiply, then divide.
โ Incorrect
(4 + 3) = 7, (4 - 3) = 1, so 7 * 1 = 7, then 7 / 3 = 2 (integer division).
โ Predict Output
expert2:00remaining
What is the output of this Rust code involving overflow with wrapping arithmetic?
Consider this Rust code using wrapping_add. What will it print?
Rust
fn main() {
let max = u8::MAX; // 255
let result = max.wrapping_add(10);
println!("{}", result);
}Attempts:
2 left
๐ก Hint
Wrapping addition wraps around on overflow instead of panicking.
โ Incorrect
u8 max is 255. Adding 10 wraps around: 255 + 10 = 265 mod 256 = 9.