FromIterator
์ด๋ค ์ปฌ๋ ์
์ด FromIterator
๋ฅผ ๊ตฌํํ๊ณ ์๋ค๋ฉด Iterator
๋ก๋ถํฐ ๊ทธ ์ปฌ๋ ์
์ ๋ง๋ค ์ ์์ต๋๋ค.
fn main() { let primes = vec![2, 3, 5, 7]; let prime_squares = primes.into_iter().map(|p| p * p).collect::<Vec<_>>(); println!("prime_squares: {prime_squares:?}"); }
This slide should take about 5 minutes.
Iterator
implements
fn collect<B>(self) -> B
where
B: FromIterator<Self::Item>,
Self: Sized
์ด ๋ฉ์๋์ B
๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์๋ ๋ ๊ฐ์ง๊ฐ ์์ต๋๋ค.
- With the โturbofishโ:
some_iterator.collect::<COLLECTION_TYPE>()
, as shown. The_
shorthand used here lets Rust infer the type of theVec
elements. - ํ์
์ถ๋ก ์ฌ์ฉ:
let prime_squares: Vec<_> = some_iterator.collect()
๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด ๋ฐฉ๋ฒ์ผ๋ก ์์ ๋ฅผ ๋ค์ ์์ฑํด ๋ณด์ธ์.
There are basic implementations of FromIterator
for Vec
, HashMap
, etc. There are also more specialized implementations which let you do cool things like convert an Iterator<Item = Result<V, E>>
into a Result<Vec<V>, E>
.