9951 explained code solutions for 126 technologies


rustRust named loop example


A named loop in Rust is a loop that can be labeled with a name. This allows the loop to be breaked or continueed from anywhere in the code.

Example code

'outer: loop {
    println!("Entered the outer loop");

    'inner: loop {
        println!("Entered the inner loop");

        // This would break only the inner loop
        break 'inner;

        println!("This point will never be reached");
    }

    println!("This point is reached");

    // This breaks the outer loop
    break 'outer;
}

println!("Exited the outer loop");

Output example

Entered the outer loop
Entered the inner loop
This point is reached
Exited the outer loop

Code explanation

  1. 'outer: loop - This is the outer loop, labeled with the name outer.
  2. break 'inner - This statement breaks the inner loop, labeled with the name inner.
  3. break 'outer - This statement breaks the outer loop, labeled with the name outer.

Helpful links

Edit this code on GitHub