<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 循環 > [loops.md](https://github.com/rust-lang/rust/blob/master/src/doc/book/loops.md) commit 2217cf1af27d7980aba9deca4e78165cab5e80fc Rust 目前提供 3 種方法來進行一些迭代操作。他們是`loop`,`while`和`for`。每種方法都有自己的用途。 ### loop 無限`loop`是 Rust 提供的最簡單的循環。使用`loop`關鍵字,Rust 提供了一個直到一些終止語句被執行的循環方法。Rust 的無限`loop`看起來像這樣: ~~~ loop { println!("Loop forever!"); } ~~~ ### while Rust 也有一個`while`循環。它看起來像: ~~~ let mut x = 5; // mut x: i32 let mut done = false; // mut done: bool while !done { x += x - 3; println!("{}", x); if x % 5 == 0 { done = true; } } ~~~ `while`循環是當你不確定應該循環多少次時正確的選擇。 如果你需要一個無限循環,你可能想要這么寫: ~~~ while true { ~~~ 然而,`loop`遠比它適合處理這個情況: ~~~ loop { ~~~ Rust 的控制流分析會區別對待這個與`while true`,因為我們知道它會一直循環。現階段理解這些細節*意味著*什么并不是非常重要,基本上,你給編譯器越多的信息,越能確保安全和生成更好的代碼,所以當你打算無限循環的時候應該總是傾向于使用`loop`。 ### for `for`用來循環一個特定的次數。然而,Rust的`for`循環與其它系統語言有些許不同。Rust的`for`循環看起來并不像這個“C語言樣式”的`for`循環: ~~~ for (x = 0; x < 10; x++) { printf( "%d\n", x ); } ~~~ 相反,它看起來像這個樣子: ~~~ for x in 0..10 { println!("{}", x); // x: i32 } ~~~ 更抽象的形式: ~~~ for var in expression { code } ~~~ 這個表達式是一個[迭代器](#).迭代器返回一系列的元素。每個元素是循環中的一次重復。然后它的值與`var`綁定,它在循環體中有效。每當循環體執行完后,我們從迭代器中取出下一個值,然后我們再重復一遍。當迭代器中不再有值時,`for`循環結束。 在我們的例子中,`0..10`表達式取一個開始和結束的位置,然后給出一個含有這之間值得迭代器。當然它不包括上限值,所以我們的循環會打印`0`到`9`,而不是到`10`。 Rust 沒有使用“C語言風格”的`for`循環是有意為之的。即使對于有經驗的 C 語言開發者來說,要手動控制要循環的每個元素也都是復雜并且易于出錯的。 ### Enumerate方法 當你需要記錄你已經循環了多少次了的時候,你可以使用`.enumerate()`函數。 ### 對范圍(On ranges): ~~~ for (i,j) in (5..10).enumerate() { println!("i = {} and j = {}", i, j); } ~~~ 輸出: ~~~ i = 0 and j = 5 i = 1 and j = 6 i = 2 and j = 7 i = 3 and j = 8 i = 4 and j = 9 ~~~ 別忘了在范圍外面加上括號。 ### 對迭代器(On iterators): ~~~ # let lines = "hello\nworld".lines(); for (linenumber, line) in lines.enumerate() { println!("{}: {}", linenumber, line); } ~~~ 輸出: ~~~ 0: hello 1: world ~~~ ### 提早結束迭代(Ending iteration early) 讓我們再看一眼之前的`while`循環: ~~~ let mut x = 5; let mut done = false; while !done { x += x - 3; println!("{}", x); if x % 5 == 0 { done = true; } } ~~~ 我們必須使用一個`mut`布爾型變量綁定,`done`,來確定何時我們應該推出循環。Rust 有兩個關鍵字幫助我們來修改迭代:`break`和`continue`。 這樣,我們可以用`break`來寫一個更好的循環: ~~~ let mut x = 5; loop { x += x - 3; println!("{}", x); if x % 5 == 0 { break; } } ~~~ 現在我們用`loop`來無限循環,然后用`break`來提前退出循環。 `continue`比較類似,不過不是退出循環,它直接進行下一次迭代。下面的例子只會打印奇數: ~~~ for x in 0..10 { if x % 2 == 0 { continue; } println!("{}", x); } ~~~ `break`和`continue`在`while`循環和[`for`循環](#)中都有效。 ### 循環標簽(Loop labels) 你也許會遇到這樣的情形,當你有嵌套的循環而希望指定你的哪一個`break`或`continue`該起作用。就像大多數語言,默認`break`或`continue`將會作用于最內層的循環。當你想要一個`break`或`continue`作用于一個外層循環,你可以使用標簽來指定你的`break`或`continue`語句作用的循環。如下代碼只會在`x`和`y`都為奇數時打印他們: ~~~ 'outer: for x in 0..10 { 'inner: for y in 0..10 { if x % 2 == 0 { continue 'outer; } // continues the loop over x if y % 2 == 0 { continue 'inner; } // continues the loop over y println!("x: {}, y: {}", x, y); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看