0032-Rust-自实现迭代器

环境

  • Time 2022-05-21
  • Rust 1.61.0

前言

说明

参考:https://doc.rust-lang.org/std/iter/index.html

目标

接前一节,在迭代的过程中,修改每个迭代的元素。

自定义类型

#[derive(Debug)]
struct MyData<T: Debug> {
    value1: T,
    value2: T,
    value3: T,
    value4: T,
}

new

impl<T: Debug> MyData<T> {
    fn new(v1: T, v2: T, v3: T, v4: T) -> Self {
        Self {
            value1: v1,
            value2: v2,
            value3: v3,
            value4: v4,
        }
    }
}

实现迭代器

struct Iter<T>(Vec<T>);

impl<T> Iterator for Iter<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.pop()
    }
}

生成迭代器

impl<T: Debug> IntoIterator for MyData<T> {
    type Item = T;
    type IntoIter = Iter<T>;
    fn into_iter(self) -> Iter<T> {
        let vec = vec![self.value4, self.value3, self.value2, self.value1];
        Iter(vec)
    }
}

不取所有权

impl<T: Debug> MyData<T> {
    fn iter(&self) -> Iter<&T> {
        let vec = vec![&self.value4, &self.value3, &self.value2, &self.value1];
        Iter(vec)
    }
}

引用的迭代

impl<'a, T: Debug> IntoIterator for &'a MyData<T> {
    type Item = &'a T;
    type IntoIter = Iter<&'a T>;
    fn into_iter(self) -> Iter<&'a T> {
        self.iter()
    }
}

impl<'a, T: Debug> IntoIterator for &'a mut MyData<T> {
    type Item = &'a mut T;
    type IntoIter = Iter<&'a mut T>;
    fn into_iter(self) -> Iter<&'a mut T> {
        self.iter_mut()
    }
}

使用迭代器

fn main() {
    let mut my_data = MyData::new(88, 44, 66, 22);
    for value in &mut my_data {
        *value += 33;
    }
    println!("my data:{my_data:?}");

    for value in my_data {
        println!("value: {value:?}")
    }
    // 编译错误,已经被移动
    // println!("my data:{my_data:?}");
}

总结

实现了不取走所有权的迭代器,在迭代的过程中,修改每个元素。

附录

源码

use std::fmt::Debug;

fn main() {
    let mut my_data = MyData::new(88, 44, 66, 22);
    for value in &mut my_data {
        *value += 33;
    }
    println!("my data:{my_data:?}");

    for value in my_data {
        println!("value: {value:?}")
    }
    // 编译错误,已经被移动
    // println!("my data:{my_data:?}");
}

#[derive(Debug)]
struct MyData<T: Debug> {
    value1: T,
    value2: T,
    value3: T,
    value4: T,
}

impl<T: Debug> MyData<T> {
    fn new(v1: T, v2: T, v3: T, v4: T) -> Self {
        Self {
            value1: v1,
            value2: v2,
            value3: v3,
            value4: v4,
        }
    }
}

impl<T: Debug> MyData<T> {
    fn iter(&self) -> Iter<&T> {
        let vec = vec![&self.value4, &self.value3, &self.value2, &self.value1];
        Iter(vec)
    }

    fn iter_mut(&mut self) -> Iter<&mut T> {
        let vec = vec![
            &mut self.value4,
            &mut self.value3,
            &mut self.value2,
            &mut self.value1,
        ];
        Iter(vec)
    }
}

impl<T: Debug> IntoIterator for MyData<T> {
    type Item = T;
    type IntoIter = Iter<T>;
    fn into_iter(self) -> Iter<T> {
        let vec = vec![self.value4, self.value3, self.value2, self.value1];
        Iter(vec)
    }
}

impl<'a, T: Debug> IntoIterator for &'a MyData<T> {
    type Item = &'a T;
    type IntoIter = Iter<&'a T>;
    fn into_iter(self) -> Iter<&'a T> {
        self.iter()
    }
}

impl<'a, T: Debug> IntoIterator for &'a mut MyData<T> {
    type Item = &'a mut T;
    type IntoIter = Iter<&'a mut T>;
    fn into_iter(self) -> Iter<&'a mut T> {
        self.iter_mut()
    }
}

struct Iter<T>(Vec<T>);

impl<T> Iterator for Iter<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.pop()
    }
}
posted @ 2022-08-29 17:59  jiangbo4444  阅读(76)  评论(0编辑  收藏  举报