Mastering Rust Loop Variants: loop, while, and for

preview_player
Показать описание
In Rust, there are three main loop constructs: loop, while, and for. Here is a brief description of each:

loop: This is an infinite loop. It will execute the block of code within it indefinitely until a break statement is encountered.

while: This loop iterates over a block of code as long as a specified condition is true. It stops when the condition becomes false.

for: This loop iterates over a range, collection, or iterator. It's the preferred choice when iterating through a sequence of elements.

This code demonstrates each loop variant in Rust. First, we have an infinite loop, which runs until counter reaches 5. Next, we use a while loop to countdown from 3 to 1. Finally, we use a for loop to iterate over a range and print the square of each number in the range.
Рекомендации по теме
visit shbcf.ru