Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Rust Loops

Rust Loops

Besides flexible conditional statements, Rust's loop structures are also very mature in design. As a battle-tested developer, you should be able to feel this.

while loop

The while loop is the most typical conditional loop:

Example

fn

main

(

)

{

   

let

mut

number

=

1

;

   

while

number

!=

4

{

        println

!

(

"{}"

,

number

)

;

        number

+=

1

;

   

}

    println

!

(

"EXIT"

)

;

}

Running result:

1

2

3

EXIT

As of the writing of this tutorial, the Rust language does not yet have do-while, but do is designated as a reserved word and may be used in future versions.

In C, the for loop uses a ternary statement to control looping, but Rust has no such usage; you need to use a while loop instead:

C language

int

i

;

for

(

i

=

0

;

i

<

10

;

i

++

)

{

   

// Loop body

}

Rust

let

mut

i

=

0

;

while

i

<

10

{

// Loop body

    i

+=

1

;

}

for loop

The for loop is the most commonly used loop structure, often used to traverse a linear data structure (such as an array). A for loop traversing an array:

Example

fn

main

(

)

{

   

let

a

=

[

10

,

20

,

30

,

40

,

50

]

;

   

for

i

in

a.iter

(

)

{

        println

!

(

"Value is : {}"

,

i

)

;

   

}

}

Running result:

值为 : 10

值为 : 20

值为 : 30

值为 : 40

值为 : 50

The for loop in this program completes the traversal of array a. a.iter() represents a's iterator; we won't elaborate until the chapter on objects.

Of course, a for loop can actually access an array via indices:

Example

fn

main

(

)

{

let

a

=

[

10

,

20

,

30

,

40

,

50

]

;

   

for

i

in

0

..

5

{

        println

!

(

"a[{}] = {}"

,

i

,

a

[

i

]

)

;

   

}

}

Running result:

a[0] = 10

a[1] = 20

a[2] = 30

a[3] = 40

a[4] = 50

The loop loop

Battle-tested developers must have encountered this situation several times: a loop cannot determine at the beginning or end whether to continue, and must control its progress somewhere in the middle of the loop body. In such cases, we often implement an early exit inside a while (true) loop body.

The Rust language has a native infinite loop structure — loop:

Example

fn

main

(

)

{

   

let

s

=

[

'R'

,

'U'

,

'N'

,

'O'

,

'O'

,

'B'

]

;

   

let

mut

i

=

0

;

   

loop

{

       

let

ch

=

s

[

i

]

;

       

if

ch

==

'O'

{

           

break

;

       

}

        println

!

(

"'{}'"

,

ch

)

;

        i

+=

1

;

   

}

}

Running result:



'R' 

'U' 

'N' 

A loop can use the break keyword, similar to return, to exit the entire loop and give a value to the outside. This is a very clever design, because loops like this are often used as search tools; if something is found, of course the result should be handed out:

Example

fn

main

(

)

{

   

let

s

=

[

'R'

,

'U'

,

'N'

,

'O'

,

'O'

,

'B'

]

;

   

let

mut

i

=

0

;

   

let

location

=

loop

{

       

let

ch

=

s

[

i

]

;

       

if

ch

==

'O'

{

           

break

i

;

       

}

        i

+=

1

;

   

}

;

    println

!

(

"Index of 'O' is {}"

,

location

)

;

}

Running result:

 'O' 的索引为 3