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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Vectors > [vectors.md](https://github.com/rust-lang/rust/blob/master/src/doc/book/vectors.md) commit 5b9dd6a016adb5ed67e150643fb7e21dcc916845 “Vector”是一個動態或“可增長”的數組,被實現為標準庫類型[`Vec<T>`](http://doc.rust-lang.org/std/vec/)(其中`<T>`是一個[泛型](#)語句)。vector總是在堆上分配數據。vector與切片就像`String`與`&str`一樣。你可以使用`vec!`宏來創建它: ~~~ let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32> ~~~ (與之前使用`println!`宏時不一樣,我們用中括號`[]`配合`vec!`。為了方便,Rust 允許使用上述各種情況。) 對于重復初始值有另一種形式的`vec!`: ~~~ let v = vec![0; 10]; // ten zeroes ~~~ ### 訪問元素 為了vector特定索引的值,使用`[]`: ~~~ let v = vec![1, 2, 3, 4, 5]; println!("The third element of v is {}", v[2]); ~~~ 索引從`0`開始,所以第3個元素是`v[2]`。 另外值得注意的是必須用`usize`類型的值來索引: ~~~ let v = vec![1, 2, 3, 4, 5]; let i: usize = 0; let j: i32 = 0; // works v[i]; // doesn’t v[j]; ~~~ 用非`usize`類型索引的話會給出類似如下的錯誤: ~~~ error: the trait `core::ops::Index` is not implemented for the type `collections::vec::Vec<_>` [E0277] v[j]; ^~~~ note: the type `collections::vec::Vec<_>` cannot be indexed by `i32` error: aborting due to previous error ~~~ 信息中有很多標點符號,不過關鍵是:你不能用`i32`來索引。 ### 越界訪問(Out-of-bounds Access) 如果嘗試訪問并不存在的索引: ~~~ let v = vec![1, 2, 3]; println!("Item 7 is {}", v[7]); ~~~ 那么當前的線程會 [panic](#)并輸出如下信息: ~~~ thread '' panicked at 'index out of bounds: the len is 3 but the index is 7' ~~~ 如果你想處理越界錯誤而不是 panic,你可以使用像[`get`](http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get)或[`get_mut`](http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get)這樣的方法,他們當給出一個無效的索引時返回`None`: ~~~ let v = vec![1, 2, 3]; match v.get(7) { Some(x) => println!("Item 7 is {}", x), None => println!("Sorry, this vector is too short.") } ~~~ ### 迭代 可以用`for`來迭代 vector 的元素。有3個版本: ~~~ let mut v = vec![1, 2, 3, 4, 5]; for i in &v { println!("A reference to {}", i); } for i in &mut v { println!("A mutable reference to {}", i); } for i in v { println!("Take ownership of the vector and its element {}", i); } ~~~ vector還有很多有用的方法,可以看看[vector的API文檔](http://doc.rust-lang.org/nightly/std/vec/)了解它們。
                  <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>

                              哎呀哎呀视频在线观看