rust - 将迭代器返回给 RefCell

标签 rust iterator lifetime

我不知道如何在以下代码片段中实现 children_iter:

  use std::{cell::RefCell, rc::Rc};                                                                                                                                                                          
                                                                                                                                                                                                             
  struct Node {                                                                                                                                                                                              
      children: Vec<Rc<RefCell<Node>>>,                                                                                                                                                                      
  }                                                                                                                                                                                                          
                                                                                                                                                                                                             
  struct NodeIterator {                                                                                                                                                                                      
      current: Rc<RefCell<Node>>,                                                                                                                                                                            
  }                                                                                                                                                                                                          
                                                                                                                                                                                                             
  impl NodeIterator {                                                                                                                                                                                        
      fn new(node: Rc<RefCell<Node>>) -> NodeIterator {                                                                                                                                                      
          NodeIterator { current: node }                                                                                                                                                                     
      }                                                                                                                                                                                                      
           
      /// Returns an iterator over the children of the current node wrapped in a NodeIterator                                                                                                                                                                                                  
      fn children_iter(&self) -> impl Iterator<Item = NodeIterator> {                                                                                                                                         
          self.current ‣Rc<RefCell<Node>>                                                                                                                                                                    
              .borrow() ‣Ref<Node>                                                                                                                                                                           
              .children ‣Vec<Rc<RefCell<Node>>>                                                                                                                                                              
              .iter() ‣Iter<Rc<RefCell<Node>>>                                                                                                                                                               
              .map(|n| Self::new(n.clone())) ‣&Rc<RefCell<Node>>                                                                                                                                             
      }                                                                                                                                                                                                      
  }                                                                                                                                                                                                          
              

问题是我不确定返回的迭代器迭代时应该包含哪些子项。我尝试了一些不同的东西:

  1. 也许我可以将迭代器绑定(bind)到 self 的生命周期?:
fn children_iter<'a>(&'a self) -> impl 'a + Iterator<Item=NodeIterator>

这失败了,因为它返回了当前函数拥有的数据

  1. 也许我可以让 self 被消费,这样我们就不必保留对它的任何引用?
fn children_iter(self) -> impl Iterator<Item=NodeIterator>

这失败了 self.current does not live long enough

  1. 我还尝试克隆 Rc,这样它就不会引用 self 中的那个
self.current
    .clone()
    .borrow()
    ......

这失败了,“创建了一个仍在使用中被释放的临时文件”

最佳答案

想通了:

self.current
    .borrow()
    .children()
    .clone()
    .into_iter()
    .map(|n| Self::new(n))

这样你就可以返回一个克隆的Vec,它已经变成了一个迭代器

关于rust - 将迭代器返回给 RefCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65866494/

相关文章:

readline - rust 0.10 有 rl 包吗?

generics - 创建新的通用结构的正确方法是什么?

c++ - 使用字符串反向迭代器和迭代器作为相同类型的对象(C++)

rust - 闭合体的生命周期与传递给它的值之间不匹配

macros - 在生成的函数中使用宏类型参数

rust - 是否可以为由所有实现特征的类型组成的任何元组自动实现特征?

c++ - 如何在基于范围的for循环中找到当前对象的索引?

python - 生成器在 Python 中解包的奇怪行为

hashmap - 对包含引用的临时值强制执行生存期

rust - 可变引用的再借用