vec!
: macro
let vec_name = vec![elem1, elem2, elem3];
let name : Vec <typesize> = vec!elem1, elem2, elem3];
<>
괄호로 묶인 벡터의 type과 size를 정의하는 것은 선택 사항vec!
macro 사용할 것 fn main() {
//define a vector of size 4
let my_vec = vec![1, 2, 3, 4, 5];
//print the vector
println!("{:?}", my_vec);
}
-----------------------------
* console
[1, 2, 3, 4, 5]
note
- Like arrays can be displayed on the screen using the
println!()
macro.
-[]
를 통해 해당 value에 접근 할 수 있음
fn main() {
let my_vec = vec![1,2,3,4,5];
println!("{}", my_vec[0]);
}
fn main() {
let my_vec = vec![1,2,3,4,5];
println!("{}", my_vec[9]);
}
--------------------------------------
*console
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 9', /rustc/73528e339aae0f17a15ffa49a8ac608f50c6cf14/src/libcore/slice/mod.rs:2796:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
범위를 벗어난 예외를 처리하기 위해 None키워드를 사용할 수 있음(아래 예시)
fn main() {
let my_vec = vec![1, 2, 3,4,5];
match my_vec.get(9) {
Some(x) => println!("Value at given index:{}", x),
None => println!("Sorry, you are accessing a value out of bound")
}
}
fn main() {
println!("Print using debug trait");
let my_vec = vec![1, 2, 3,4,5];
//using debug trait
println!("Vector : {:?}", my_vec);
println!("Print using for loop");
// using loop
let mut index = 0;
for i in my_vec {
println!("Element at index {}:{} ", index, i);
index = index+1;
}
}
fn main() {
let mut my_vec = Vec::new();
println!("Empty Vector : {:?}", my_vec);
my_vec.push(1);
my_vec.push(2);
my_vec.push(3);
println!("Pushed elements 1 , 2 , 3 : {:?}", my_vec);
my_vec.pop();
println!("Popped value: {}", 3);
println!("Popped element at last index : {:?}", my_vec);
my_vec.remove(1);
println!("Removed value: {}", 2);
println!("Removed element at index 1 : {:?}", my_vec);
println!("Size of vector is :{}", my_vec.len());
println!("Does my vector contains 1 : {}", my_vec.contains(&1));
}
note
.contains
function을 사용할 때 value를 borrowing하는 것을 고려할 것
push
method를 이용해 vector에 요소 추가하기fn main() {
// define a vector of size 5
let mut my_vec = vec![1, 2, 3, 4, 5];
// print vector
println!("Vector : {:?}", my_vec);
// 용량
println!("Capacity of vector: {}", my_vec.capacity());
// 길이
println!("Length of the vector : {}",my_vec.len());
my_vec.push(6);
my_vec.push(8);
// print vector
println!("Vector : {:?}",my_vec);
// 용량
println!("Capacity of vector: {}", my_vec.capacity());
// 길이
println!("Length of the vector : {}", my_vec.len());
}
pop
: 끝에서부터 지우기 remove()
: 특정 인덱스 지우기 fn main() {
// define a vector of size 5
let mut my_vec = vec![1, 2, 3, 4, 5];
my_vec.pop();
my_vec.pop();
// print vector
println!("Vector : {:?}",my_vec);
// print the capacity of vector
println!("Capacity of vector: {}", my_vec.capacity());
// print the length of vector
println!("Length of the vector : {}", my_vec.len());
}
---------------------------------------------------
*console
Vector : [1, 2, 3, 4, 5]
Capacity of vector: 5
Length of the vector : 5
Vector : [1, 2, 3]
Capacity of vector: 5 //왜 5일까?
Length of the vector : 3
iter()
: built-in function .position
: built-in function(|&e| e == element_name)
은 우리가 찾고자 하는 요소의 이름과 동일한 값을 가진 변수 e
를 정의합니다.fn main() {
// defines a mutable vector
let mut my_vec = vec![1, 2, 3, 4, 5];
// define the value to be removed
let value = 2;
// get the index of the value in the vector
let index = my_vec.iter().position(|&r| r == value).unwrap();
// call the built-in remove method
my_vec.remove(index);
// print the updated vector
println!("Updated Vector: {:?}", my_vec);
}
--------------------------------------------
*console
Updated Vector: [1, 3, 4, 5]
.iter()
를 사용하여 순회할 수 있음fn main() {
// define a vector of size 5
let my_vec = vec![1, 2, 3, 4, 5];
// using loop
let mut index = 0;
for i in my_vec.iter(){ // it works even if .iter() is not written
println!("Element at index {}:{} ", index, i);
index = index + 1;
}
}
.iter_mut()
를 사용하여 변경할 수 있음slice
: 두 단어로 구성된 object(객체)let slice_vector:&[i32] = &vec[0..2];
fn main() {
// define a vector of size 5
let my_vec = vec![1, 2, 3, 4, 5];
let slice:&[i32] = &my_vec[2..4];
// print the vector
println!("Slice of the vector : {:?}",slice);
}