title | date | draft |
---|---|---|
[Rust学习]创建Vec指定长度 |
2018-06-27 |
false |
golang中可创建指定长度的切片,Rust中的Vec只能在创建时指定容量。
let v: Vec<i32> = Vec::with_capacity(3);
println!("{}", v[2]);// index out of bounds
let mut v: Vec<i32> = Vec::new();
unsafe { v.set_len(3); }
println!("v:{}", v[2]);// SIGSEGV, 段错误
点进去发现,
/// Sets the length of a vector.
///
/// This will explicitly set the size of the vector, without actually
/// modifying its buffers, so it is up to the caller to ensure that the
/// vector is actually the specified size.
因此实用 set_len
需要确保容量
let mut v: Vec<i32> = Vec::with_capacity(3);
unsafe { v.set_len(3); }
println!("v:{}", v[2]); // v:0
let v = vec![0;10];
let v = std::vec::from_elem(0, 10);