loop - Rust
Description: API documentation for the Rust `loop` keyword in crate `std`.
Keywords: rust, rustlang, rust-lang, loop

# [−]Keyword loop

The loop-defining keyword.

`loop` is used to define the simplest kind of loop supported in Rust. It runs the code inside it until the code uses `break` or the program exits.

    loop {
        println!("hello world forever!");
    }

    let mut i = 0;
    loop {
        println!("i is {}", i);
        if i > 10 {
            break;
        }
        i += 1;
    }Run

Unlike the other kinds of loops in Rust (`while`, `while let`, and `for`), loops can be used as expressions that return values via `break`.

    let mut i = 1;
    let something = loop {
        i *= 2;
        if i > 100 {
            break i;
        }
    };
    assert_eq!(something, 128);Run

Every `break` in a loop has to have the same type. When it's not explicitly giving something, `break;` returns `()`.

For more information on `loop` and loops in general, see the Reference.

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)