Rust Continue Statement

In this tutorial you will learn about the Rust Continue Statement and its application with practical example.

Rust Continue Statement

In Rust, the continue statement gives you way to skip over the current iteration of any loop. When a continue statement is encountered in the loop, the rest of statements in the loop body for current iteration and returns the program execution to the very first statement in the loop body. It does not terminates the loop rather continues with the next iteration.

Rust Continue Statement Flow Diagram

rust_continue_flow_diagram

Syntax:-

Example:-

When we run the above Rust program, we will see following output –

Output:-

rust_continue_statement

As you can see when ctr == 5, continue statement is executed which causes the current iteration to end and the control moves on to the next iteration.

Rust Labeled Continue Statement

In Rust, sometimes you may encounter situations where you have nested loops, in such case you are required to specify the loop which one your continue statement is applicable for. The standard unlabeled continue statement is applicable for the nearest enclosing loop. In Rust, there is another form of continue(labeled continue) statement is used to continue outer loop. In such cases, the loops must be annotated with some ‘label, which is passed to the continue statement.

Example:-

Output:-

rust_labeled_continue_statement

In this tutorial we have learn about the Rust Continue Statement and its application with practical example. I hope you will like this tutorial.