<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                目前唯一穩定的創建`Box`的方法是通過`Box::new`方法。并且不可能在一個模式匹配中穩定的析構一個`Box`。不穩定的`box`關鍵字可以用來創建和析構`Box`。下面是一個用例: ~~~ #![feature(box_syntax, box_patterns)] fn main() { let b = Some(box 5); match b { Some(box n) if n < 0 => { println!("Box contains negative number {}", n); }, Some(box n) if n >= 0 => { println!("Box contains non-negative number {}", n); }, None => { println!("No box"); }, _ => unreachable!() } } ~~~ 注意這些功能目前隱藏在`box_syntax`(裝箱創建)和`box_patterns`(析構和模式匹配)通道之中因為它的語法在未來可能會改變。 ## 返回指針 在很多有指針的語言中,你的函數可以返回一個指針來避免拷貝大的數據結構。例如: ~~~ struct BigStruct { one: i32, two: i32, // etc one_hundred: i32, } fn foo(x: Box<BigStruct>) -> Box<BigStruct> { Box::new(*x) } fn main() { let x = Box::new(BigStruct { one: 1, two: 2, one_hundred: 100, }); let y = foo(x); } ~~~ 要點是通過傳遞一個裝箱,你只需拷貝了一個指針,而不是那構成了`BigStruct`的一百個`int`值。 上面是Rust中的一個反模式。相反,這樣寫: ~~~ #![feature(box_syntax)] struct BigStruct { one: i32, two: i32, // etc one_hundred: i32, } fn foo(x: Box<BigStruct>) -> BigStruct { *x } fn main() { let x = Box::new(BigStruct { one: 1, two: 2, one_hundred: 100, }); let y: Box<BigStruct> = box foo(x); } ~~~ 這在不犧牲性能的前提下獲得了靈活性。 你可能會認為這會給我們帶來很差的性能:返回一個值然后馬上把它裝箱?難道這在哪里不都是最糟的嗎?Rust顯得更聰明。這里并沒有拷貝。`main`為裝箱分配了足夠的空間,向`foo`傳遞一個指向他內存的`x`,然后`foo`直接向`Box`中寫入數據。 因為這很重要所以要說兩遍:返回指針會阻止編譯器優化你的代碼。允許調用函數選擇它們需要如何使用你的輸出。
                  <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>

                              哎呀哎呀视频在线观看