trait_Iterator

trait_Iterator (반복자 트레이트)

반복자를 다루기 위한 핵심 트레이트예요. next()를 구현해 값을 차례로 생성하는 가장 기본적인 반복자 트레이트랍니다.

출처: Rust 공식 문서

본문

이것은 주요 반복자 트레이트예요. 반복자라는 개념에 대해 더 자세히 알고 싶다면 모듈 수준 문서를 참고하세요. 특히 Iterator를 어떻게 구현하는지 알고 싶을 수 있어요.

시그니처는 다음과 같아요. (아래는 핵심 메서드만 발췌했어요. 전체 76개 메서드 목록은 공식 문서를 참고하세요.)

pub trait Iterator {
    type Item;

    // 필수 메서드
    fn next(&mut self) -> Option<Self::Item>;

    // 제공되는 주요 메서드 (일부)
    fn size_hint(&self) -> (usize, Option<usize>) { ... }
    fn count(self) -> usize
       where Self: Sized { ... }
    fn last(self) -> Option<Self::Item>
       where Self: Sized { ... }
    fn nth(&mut self, n: usize) -> Option<Self::Item> { ... }
    fn step_by(self, step: usize) -> StepBy<Self> ⓘ
       where Self: Sized { ... }
    fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> ⓘ
       where Self: Sized,
             U: IntoIterator<Item = Self::Item> { ... }
    fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> ⓘ
       where Self: Sized,
             U: IntoIterator { ... }
    fn map<B, F>(self, f: F) -> Map<Self, F> ⓘ
       where Self: Sized,
             F: FnMut(Self::Item) -> B { ... }
    fn for_each<F>(self, f: F)
       where Self: Sized,
             F: FnMut(Self::Item) { ... }
    fn filter<P>(self, predicate: P) -> Filter<Self, P> ⓘ
       where Self: Sized,
             P: FnMut(&Self::Item) -> bool { ... }
    fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> ⓘ
       where Self: Sized,
             F: FnMut(Self::Item) -> Option<B> { ... }
    fn enumerate(self) -> Enumerate<Self> ⓘ
       where Self: Sized { ... }
    fn peekable(self) -> Peekable<Self> ⓘ
       where Self: Sized { ... }
    fn skip(self, n: usize) -> Skip<Self> ⓘ
       where Self: Sized { ... }
    fn take(self, n: usize) -> Take<Self> ⓘ
       where Self: Sized { ... }
    fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> ⓘ
       where Self: Sized,
             U: IntoIterator,
             F: FnMut(Self::Item) -> U { ... }
    fn flatten(self) -> Flatten<Self> ⓘ
       where Self: Sized,
             Self::Item: IntoIterator { ... }
    fn fuse(self) -> Fuse<Self> ⓘ
       where Self: Sized { ... }
    fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
       where Self: Sized,
             F: FnMut(&Self::Item) { ... }
    fn by_ref(&mut self) -> &mut Self
       where Self: Sized { ... }
    fn collect<B>(self) -> B
       where B: FromIterator<Self::Item>,
             Self: Sized { ... }
    fn partition<B, F>(self, f: F) -> (B, B)
       where Self: Sized,
             B: Default + Extend<Self::Item>,
             F: FnMut(&Self::Item) -> bool { ... }
    fn fold<B, F>(self, init: B, f: F) -> B
       where Self: Sized,
             F: FnMut(B, Self::Item) -> B { ... }
    fn reduce<F>(self, f: F) -> Option<Self::Item>
       where Self: Sized,
             F: FnMut(Self::Item, Self::Item) -> Self::Item { ... }
    fn all<F>(&mut self, f: F) -> bool
       where Self: Sized,
             F: FnMut(Self::Item) -> bool { ... }
    fn any<F>(&mut self, f: F) -> bool
       where Self: Sized,
             F: FnMut(Self::Item) -> bool { ... }
    fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
       where Self: Sized,
             P: FnMut(&Self::Item) -> bool { ... }
    fn max(self) -> Option<Self::Item>
       where Self: Sized,
             Self::Item: Ord { ... }
    fn min(self) -> Option<Self::Item>
       where Self: Sized,
             Self::Item: Ord { ... }
    fn rev(self) -> Rev<Self> ⓘ
       where Self: Sized + DoubleEndedIterator { ... }
    fn sum<S>(self) -> S
       where Self: Sized,
             S: Sum<Self::Item> { ... }
    fn product<P>(self) -> P
       where Self: Sized,
             P: Product<Self::Item> { ... }
    fn cmp<I>(self, other: I) -> Ordering
       where I: IntoIterator<Item = Self::Item>,
             Self::Item: Ord,
             Self: Sized { ... }
    fn partial_cmp<I>(self, other: I) -> Option<Ordering>
       where I: IntoIterator,
             Self::Item: PartialOrd<<I as IntoIterator>::Item>,
             Self: Sized { ... }
    fn eq<I>(self, other: I) -> bool
       where I: IntoIterator,
             Self::Item: PartialEq<<I as IntoIterator>::Item>,
             Self: Sized { ... }
}

더 알아보기 (Learn more)

Rust 공식 문서