Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let mut count = 0;
let result = loop {
count += 1;
if count == 10 {
break count * 2;
}
};
println!("{result}");
}20
- Expecting a compile error because loops have no value:
loopis an expression, andbreak count * 2makescount * 2the value of the whole loop. - Expecting
10:breaktakes the whole expression after it, sobreak count * 2leaves withcount * 2, not withcount.
- What would
resulthold if the loop ended with a barebreak;? - Why may a
whileloop not end withbreak count * 2?