pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://doc.rust-lang.org/std/pin/../default/../rc/../../../../std/ops/../../std/keyword.for.html

7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>``).join(""))

for

Keyword for 

Source
Expand description

Iteration with in, trait implementation with impl, or higher-ranked trait bounds (for<'a>).

The for keyword is used in many syntactic locations:

  • for is used in for-in-loops (see below).
  • for is used when implementing traits as in impl Trait for Type (see impl for more info on that).
  • for is also used for higher-ranked trait bounds as in for<'a> &'a T: PartialEq<i32>.

for-in-loops, or to be more precise, iterator loops, are a simple syntactic sugar over a common practice within Rust, which is to loop over anything that implements IntoIterator until the iterator returned by .into_iter() returns None (or the loop body uses break).

for i in 0..5 {
    println!("{}", i * 2);
}

for i in std::iter::repeat(5) {
    println!("turns out {i} never stops being 5");
    break; // would loop forever otherwise
}

'outer: for x in 5..50 {
    for y in 0..10 {
        if x == y {
            break 'outer;
        }
    }
}

As shown in the example above, for loops (along with all other loops) can be tagged, using similar syntax to lifetimes (only visually similar, entirely distinct in practice). Giving the same tag to break breaks the tagged loop, which is useful for inner loops. It is definitely not a goto.

A for loop expands as shown:

for loop_variable in iterator {
    code()
}
{
    let result = match IntoIterator::into_iter(iterator) {
        mut iter => loop {
            match iter.next() {
                None => break,
                Some(loop_variable) => { code(); },
            };
        },
    };
    result
}

More details on the functionality shown can be seen at the IntoIterator docs.

For more information on for-loops, see the Rust book or the Reference.

See also, loop, while.

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy