Fork me on GitHub

Project Notes

Loops in Rust

All about loops in rust

Notes

Rust supports four loop expressions:

  • A loop expression denotes an infinite loop.
  • A while expression loops until a predicate is false.
  • A while let expression tests a pattern.
  • A for expression extracts values from an iterator, looping until the iterator is empty.

All four types of loop support break expressions, continue expressions, and labels. Only loop supports evaluation to non-trivial values.

Loop

aka Infinite loops

loop {
    statements
}

While

aka Predicate loops

while condition {
    statements
}

While let

aka Predicate pattern loops

while let MatchArmPatterns = Expression {
    statements
}

For

aka Iterator loops

for Pattern in Expression {
    Statements
}

Running the Examples

Running the tests:

$ cargo test
   Compiling example v0.1.0 (.../LittleCodingKata/rust/loops/example)
    Finished test [unoptimized + debuginfo] target(s) in 0.43s
     Running target/debug/deps/example-3e96a7119aa2432d

running 5 tests
test tests::first_fib_for_works ... ok
test tests::first_fib_loop_with_return_value_works ... ok
test tests::first_fib_loop_works ... ok
test tests::first_fib_while_let_works ... ok
test tests::first_fib_while_works ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/example-362e95a34487a628

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests example

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Running the program:

$ cargo run
   Compiling example v0.1.0 (.../LittleCodingKata/rust/loops/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/example`
Loops in Rust
loop: first number in Fibonacci sequence over 10 = 13
loop_with_return_value: first number in Fibonacci sequence over 10 = 13
first_fib_while: first number in Fibonacci sequence over 10 = 13
first_fib_while_let: first number in Fibonacci sequence over 10 = 13
first_fib_for: first number in Fibonacci sequence over 10 = 13

Credits and References

About LCK#155 rust
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.