- Vectors
- 访问元素
- 越界访问(Out-of-bounds Access)
- 迭代
Vectors
vectors.md
commit 23a7a7bdb6a6a43cd7efdd9176b1d3f75d9d0e70
“Vector”是一个动态或“可增长”的数组,被实现为标准库类型Vec<T>(其中<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 将它们的内容以连续的T的数组的形式存储在堆上,这意味着它们必须在编译时就知道T的大小(就是存储一个T需要多少字节)。有些类型的大小不可能在编译时就知道。为此你需要保存一个指向该类型的指针:幸好,Box类型正好适合这种情况。
访问元素
为了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 bound `collections::vec::Vec<_> : core::ops::Index<i32>`is not satisfied [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 'main' panicked at 'index out of bounds: the len is 3 but the index is 7'
如果你想处理越界错误而不是 panic,你可以使用像get或get_mut这样的方法,他们当给出一个无效的索引时返回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。例如,下面的代码不能编译。
let v = vec![1, 2, 3, 4, 5];for i in v {println!("Take ownership of the vector and its element {}", i);}for i in v {println!("Take ownership of the vector and its element {}", i);}
而如下代码则可以完美运行:
let v = vec![1, 2, 3, 4, 5];for i in &v {println!("This is a reference to {}", i);}for i in &v {println!("This is a reference to {}", i);}
vector还有很多有用的方法,可以看看vector的API文档了解它们。
