相关疑难解决方法(0)

如何使用返回可变引用的迭代器创建自己的数据结构?

我在Rust中创建了一个数据结构,我想为它创建迭代器.不可变的迭代器很容易.我目前有这个,它工作正常:

// This is a mock of the "real" EdgeIndexes class as
// the one in my real program is somewhat complex, but
// of identical type

struct EdgeIndexes;

impl Iterator for EdgeIndexes {
    type Item = usize;
    fn next(&mut self) -> Option<Self::Item> {
        Some(0)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, None)
    }
}

pub struct CGraph<E> {
    nodes: usize,
    edges: Vec<E>,
}

pub struct Edges<'a, E: 'a> {
    index: EdgeIndexes,
    graph: &'a CGraph<E>,
}

impl<'a, E> Iterator …
Run Code Online (Sandbox Code Playgroud)

lifetime rust

28
推荐指数
1
解决办法
4447
查看次数

标签 统计

lifetime ×1

rust ×1