Movatterモバイル変換


[0]ホーム

URL:


loop

Keywordloop 

Source
Expand description

Loop indefinitely.

loop is used to define the simplest kind of loop supported in Rust. It runs the code insideit until the code usesbreak or the program exits.

loop{println!("hello world forever!");}letmuti =1;loop{println!("i is {i}");ifi >100{break;    }    i*=2;}assert_eq!(i,128);

Unlike the other kinds of loops in Rust (while,while let, andfor), loops can be used asexpressions that return values viabreak.

letmuti =1;letsomething =loop{    i*=2;ifi >100{breaki;    }};assert_eq!(something,128);

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

For more information onloop and loops in general, see theReference.

See also,for,while.


[8]ページ先頭

©2009-2026 Movatter.jp