[Rust] iterators

0xDaveยท2022๋…„ 7์›” 20์ผ
0

Rust

๋ชฉ๋ก ๋ณด๊ธฐ
15/16
post-thumbnail

๐Ÿšจ ํ˜„์žฌ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ๋‚ด์šฉ์œผ๋กœ ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


iterator ๋Š” vector, ๋ฐฐ์—ด ๋“ฑ์—์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์šฉ์ดํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ข…๋ฅ˜๋กœ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  1. .iter() -> reference์˜ ํ˜•ํƒœ๋กœ iterator ์ƒ์„ฑ
  2. .iter_mut() -> mut ref์˜ ํ˜•ํƒœ๋กœ iterator ์ƒ์„ฑ
  3. .into_iter() -> ์‹ค์ œ value๋กœ iterator ์ƒ์„ฑ

์˜ˆ์‹œ๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค.

์˜ˆ์‹œ


map()

์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด map()์— ๋Œ€ํ•ด ๋จผ์ € ์‚ดํŽด๋ณด์ž.

core::iter::traits::iterator::Iterator
pub fn map<B, F>(self, f: F) -> Map<Self, F>
where
    Self: Sized,
    F: FnMut(Self::Item) -> B,

map() ์€ ๋ฐ˜๋ณต๋ฌธ์ด๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, iterator ๋’ค์— ์‚ฌ์šฉํ•˜๊ณ  FnMut Closure๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ iterator๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋‹ค.


collect

๊ทธ ๋‹ค์Œ์œผ๋กœ ์‚ดํŽด๋ณผ ๊ฒƒ์€ collect ๋‹ค.

core::iter::traits::iterator::Iterator
pub fn collect<B>(self) -> B
where
    B: FromIterator<Self::Item>,
    Self: Sized,

collect ๋Š” iterator๋ฅผ ๋ฐ›์•„์„œ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์•„์ฃผ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋‹ค. collect ๋’ค์— ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ํƒ€์ž…์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.


์ง„์งœ ์˜ˆ์ œ

ํ•„์š”ํ•œ ๊ฒƒ์€ ๋ชจ๋‘ ๋ดค์œผ๋‹ˆ ์ด์ œ ์˜ˆ์ œ๋ฅผ ๋ณด๋„๋ก ํ•˜์ž.

fn main() {
    let vector1 = vec![1, 2, 3]; 
    let vector1_a = vector1.iter().map(|x| x + 1).collect::<Vec<i32>>();
    let vector1_b = vector1.into_iter().map(|x| x * 10).collect::<Vec<i32>>();

    let mut vector2 = vec![10, 20, 30]; // we will use .iter_mut() on this one
    vector2.iter_mut().for_each(|x| *x +=100);

    println!("{:?}", vector1_a);
    println!("{:?}", vector2);
    println!("{:?}", vector1_b);
}

์ฒซ ๋ฒˆ์งธ vector1์— .iter() ๋ฅผ ์‚ฌ์šฉํ•ด์„œ map์— ์ „๋‹ฌํ•˜๊ณ , map์€ closure๋ฅผ ์ด์šฉํ•ด iterator ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์ƒ์„ฑ๋œ iterator๋ฅผ collector๊ฐ€ ๋ฐ›์•„์„œ Vec ํƒ€์ž…์œผ๋กœ ์ƒˆ๋กœ์šด vector๋ฅผ ๋งŒ๋“ค์–ด vector1_a์— ํ• ๋‹นํ•œ๋‹ค. ์ด ๋•Œ .iter() ๋Š” reference์˜ ํ˜•ํƒœ๋กœ iterator๋ฅผ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์‚ฌ์šฉํ•œ vector1์€ ๊ทธ๋Œ€๋กœ ์‚ด์•„์žˆ๋‹ค.

์ด์ œ ๋‘ ๋ฒˆ์งธ๋ฅผ ๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ์™€ ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ™์ง€๋งŒ .into_iter() ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ๋‹ค๋ฅด๋‹ค. .into_iter() ๋Š” ๊ธฐ์กด์— ์‚ฌ์šฉํ•œ vector1์„ ์†Œ๋น„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— .into_iter()๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‚˜๋ฉด ๋” ์ด์ƒ vector1์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ iter_mut() ๋ฅผ ๋ณด๋ฉด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ref ํ˜•ํƒœ๋กœ iterator๋ฅผ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— vector2๋Š” ๊ทธ๋Œ€๋กœ ์‚ด์•„์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” map() ๋Œ€์‹  for_each ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, for_each ๋Š” collect ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š” ์—†๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.


์ถœ์ฒ˜ ๋ฐ ์ฐธ๊ณ ์ž๋ฃŒ


  1. https://stackoverflow.com/questions/32872013/does-rust-have-a-way-to-apply-a-function-method-to-each-element-in-an-array-or-v
  2. https://dhghomon.github.io/easy_rust/Chapter_36.html?highlight=Iterator#iterators
  3. https://youtu.be/uHzakmeaVL8
profile
Just BUIDL :)

0๊ฐœ์˜ ๋Œ“๊ธ€