Iterator
컬λμ
μ μλ κ°λ€μ μ κ·ΌνκΈ° μν΄μλ Iterator νΈλ μμ μ¬μ©ν©λλ€. μ΄ νΈλ μμ next λ©μλλ₯Ό λΉλ‘―ν λ§μ λ©μλλ₯Ό μ 곡ν©λλ€. λ§μ νμ€ λΌμ΄λΈλ¬λ¦¬ νμ
μ΄ Iteratorλ₯Ό ꡬννκ³ μμΌλ©°, μ¬λ¬λΆλ μ¬λ¬λΆμ νμ
μ΄ μ΄ νΈλ μμ μ§μ ꡬννλλ‘ ν μ μμ΅λλ€.
struct Fibonacci { curr: u32, next: u32, } impl Iterator for Fibonacci { type Item = u32; fn next(&mut self) -> Option<Self::Item> { let new_next = self.curr + self.next; self.curr = self.next; self.next = new_next; Some(self.curr) } } fn main() { let fib = Fibonacci { curr: 0, next: 1 }; for (i, n) in fib.enumerate().take(5) { println!("fib({i}): {n}"); } }
-
The
Iteratortrait implements many common functional programming operations over collections (e.g.map,filter,reduce, etc). This is the trait where you can find all the documentation about them. In Rust these functions should produce the code as efficient as equivalent imperative implementations. -
IntoIteratorλ 루νλ₯Ό μλνκ² λ§λλ νΈλ μμ λλ€. μ΄λVec<T>μ κ°μ 컬λ μ νμ κ³Ό&Vec<T>λ°&[T]μ κ°μ μ΄μ λν μ°Έμ‘°μ μν΄ κ΅¬νλ©λλ€. λ²μλ μ΄λ₯Ό ꡬνν©λλ€. μ΄λ° μ΄μ λ‘for i in some_vec { .. }λ₯Ό μ¬μ©νμ¬ λ²‘ν°λ₯Ό λ°λ³΅ν μ μμ§λ§some_vec.next()λ μ‘΄μ¬νμ§ μμ΅λλ€.