Tsallaka zuwa abun ciki
Justice Tech School

loop, while and for

loop repeats forever until break, while checks a condition and for walks iterators. break can return a value.

Example
fn main() {
    let mut n = 0;
    let r = loop { n += 1; if n == 3 { break n * 10; } };
    println!("{}", r);
    for i in (1..=3).rev() { print!("{} ", i); }
    println!();
}

Result

30
3 2 1 

Exercise

Infinite loop.

 { break; }

Quiz Test

What can break return in loop?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.