Rust Fundamentals · 10 XP Pro
loop and break
loop repeats its block forever, until you leave it with break. It is the right
tool when you do not know in advance how many rounds you need.
let mut n = 0;
loop {
n += 1;
if n == 3 {
break;
}
}
Like if, a loop is an expression. Write a value after break and the whole
loop evaluates to it:
let mut tries = 0;
let found = loop {
tries += 1;
if tries * tries > 50 {
break tries; // the loop's value
}
};
// found == 8
Use continue to skip straight to the next round. Just make sure some path always
reaches break, or the program never ends.
Your task
Write first_power_over(base: u64, limit: u64) -> u32. It returns the smallest
exponent n (starting at 0) such that base raised to n is strictly greater
than limit. Assume base >= 2.
For example first_power_over(2, 100) is 7, because 2^7 = 128 is the first power of
two above 100. Build it with a loop that breaks with the answer.