Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Loop Desugaring

for and while loops are desugared into a conditionless loop:

#![allow(unused)]
fn main() {
for $pat in $iter {
    $loop_body
}

// becomes
{
    let mut iter = IntoIterator::into_iter($iter);
    while let Some($pat) = iter.next() {
        $loop_body
    }
}
}

And then:

#![allow(unused)]
fn main() {
while $condition {
    $loop_body
}

// becomes
loop {
    if $condition {
        $loop_body
    } else {
        break;
    }
}
}