简体   繁体   English

如何实现对二叉搜索树右边缘值的可变引用的迭代器?

[英]How to implement an iterator of mutable references to the values in the right edges of a Binary Search Tree?

I implemented a simple Binary Search Tree in Rust (following CIS 198, it's great), and for learning I'm doing iterators that just run through the right edges. 我在Rust中实现了一个简单的二进制搜索树(遵循CIS 198,它很棒),并且为了学习我正在做的迭代器只是通过正确的边缘。

I could not implement an iterator that gives mutable references. 我无法实现一个提供可变引用的迭代器。 I tried a lot of ways, but none were accepted by Rust compiler. 我尝试了很多方法,但Rust编译器都没有接受。 The code I need help is the one below ( while I made a gist with the complete code here ): 我需要帮助的代码是下面的代码( 虽然我在这里给出了完整代码的要点 ):

#[derive(Debug)]
pub struct Tree<T>(Option<Box<Node<T>>>);

#[derive(Debug)]
pub struct Node<T> {
    elem: T,
    left: Tree<T>,
    right: Tree<T>,
}

// MUTABLE BORROW STRUCT
pub struct IterMut<'a, T: 'a> {
    next: &'a mut Tree<T>,
}

// MUTABLE BORROW NEXT (I'M STUCK HERE, NOTHING WORKS)
impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        // 1 try: cannot infer lifetime
        self.next.0.as_mut().map(|node| {
            self.next = &mut node.right;
            &mut node.elem
        })

        // 2 try: node.right, node.elem does not live long enough
        self.next.0.take().map(|node| {
            self.next = &mut node.right;
            &mut node.elem
        })
    }
}

You need to change the type of the field IterMut::next to Option<&'a mut Node<T>> : 您需要更改Option<&'a mut Node<T>> IterMut::next的字段IterMut::next的类型:

pub struct IterMut<'a, T: 'a> {
    next: Option<&'a mut Node<T>>,
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        self.next.take().map(|node| {
            self.next = node.right.0.as_mut().map(|node| &mut **node);
            &mut node.elem
        })

    }
}

You can find more useful information about the implementation of the mutable iterator for recursive data structures in the IterMut chapter of Learning Rust With Entirely Too Many Linked Lists . 您可以学习Rust与完全太多链接列表的IterMut章节中找到有关递归数据结构的可变迭代器实现的更多有用信息。

认为你不能将self分成2个可变对象(一个用于Item ,一个用于self本身)而不使用一些不安全的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在可变迭代器上实现 drop_while? - How to implement drop_while on mutable iterator? 如果迭代器有生命周期,如何在可变迭代器上实现下一个方法? - How to implement the next method on mutable Iterator if the Iterator has a lifetime? 如何实现带框值的迭代器? - How to implement Iterator for boxed values? 从闭包返回并使用可变引用的迭代器 - Return and consume an iterator of mutable references from a closure 在实现返回可变引用的迭代器时,如何解决“无法为 autoref 推断适当的生命周期”? - How can I fix “cannot infer an appropriate lifetime for autoref” when implementing an iterator that returns mutable references? 如何将可变引用传递给 Rust 中迭代器循环内的函数? - How can I pass my mutable references to a function inside iterator loops in Rust? 如何使用返回可变引用的迭代器创建自己的数据结构? - How can I create my own data structure with an iterator that returns mutable references? Rust 具有许多可变引用的回溯树结构 - Rust backtrace tree structure with many mutable references 在实现二叉搜索树时,不能多次将节点借用为可变节点 - Cannot borrow node as mutable more than once while implementing a binary search tree 如何实现一个委托给 HashMap::values() 的迭代器 - how to implement an iterator that delegates to HashMap::values()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM