Vec
Vec
λ νμ ν λΉλ νμ€ κ°λ³ ν¬κΈ° λ²νΌμ
λλ€:
fn main() { let mut v1 = Vec::new(); v1.push(42); println!("v1: len = {}, μ©λ = {}", v1.len(), v1.capacity()); let mut v2 = Vec::with_capacity(v1.len() + 1); v2.extend(v1.iter()); v2.push(9999); println!("v2: len = {}, μ©λ = {}", v2.len(), v2.capacity()); // μμκ° μλ 벑ν°λ₯Ό μ΄κΈ°ννλ νμ€ λ§€ν¬λ‘μ λλ€. let mut v3 = vec![0, 0, 1, 2, 3, 4]; // μ§μ μμλ§ μ μ§ν©λλ€. v3.retain(|x| x % 2 == 0); println!("{v3:?}"); // μ°μ μ€λ³΅ μμ v3.dedup(); println!("{v3:?}"); }
Vec
μ Deref<Target = [T]>
λ₯Ό ꡬνν©λλ€. μ΄λ Vec
μμ μ¬λΌμ΄μ€ λ©μλλ₯Ό νΈμΆ ν μ μλ€λ μλ―Έμ
λλ€.
This slide should take about 10 minutes.
Vec
is a type of collection, along withString
andHashMap
. The data it contains is stored on the heap. This means the amount of data doesnβt need to be known at compile time. It can grow or shrink at runtime.Vec<T>
λ μ λ€λ¦ νμ μ΄κΈ°λ ν©λλ€. νμ§λ§T
λ₯Ό κΌ μ§μ ν΄μ€ νμλ μμ΅λλ€. μ΄ κ²½μ°, λ¬μ€νΈ νμ μΆλ‘ μ΄ λ²‘ν°μ μ²μpush
νλ λ°μ΄ν°λ‘T
λ₯Ό μ μ μμμ΅λλ€.vec![...]
λVec::new()
λμ μΈ μ μλ νμ€ λ§€ν¬λ‘λ‘μ, μ΄κΈ° λ°μ΄ν°λ₯Ό μΆκ°ν 벑ν°λ₯Ό μμ±ν μ μμ΅λλ€.- 벑ν°λ
[
]
λ₯Ό μ¬μ©νμ¬ μΈλ±μ€λ‘ μ κ·Όν μ μμ΅λλ€. νμ§λ§ λ²μλ₯Ό λ²μ΄λλ©΄ ν¨λμ΄ λ°μν©λλ€. λμget
μ μ¬μ©νλ©΄Option
μ λ°νν©λλ€.pop
ν¨μλ λ§μ§λ§ μμλ₯Ό μ κ±°ν©λλ€. - Slices are covered on day 3. For now, students only need to know that a value of type
Vec
gives access to all of the documented slice methods, too.