Rust Double Linked List exercise

Hi! I'm very new of Rust and I'm still learning. To better understand the smartpointer I was implementing the double linked list as exercise but I came across the following problem:

Having this code:

enum element<T: Display + PartialEq> {
    Next(Rc<RefCell<DoubleLinkedList<T>>>),
    Prev(Weak<RefCell<DoubleLinkedList<T>>>),
    Nil,
}

struct DoubleLinkedList<T: Display + PartialEq>{
    value: T, 
    next: element<T>,
    prev: element<T>,
}
// Append at the end of the list
    fn append(&mut self, value: T, prev: &element<T>){
        match &mut self.next {
            element::Next(v) => v.borrow_mut().append(value, &self.next),
            element::Nil => {
                let mut d = DoubleLinkedList::new(value);
                if let element::Nil = prev{
                    d.prev = element::Nil;
                } else if let element::Next(v) = prev {
                    d.prev = element::Prev(Rc::downgrade(v));
                } else {
                    d.prev = element::Nil;
                }
                self.next = element::Next(Rc::new(RefCell::new(d)));
            },
            element::Prev(_) => ()
        }
    }

When I append a new node at the end of the list, I would like to have a backpointer to the previous node by using a Weak pointer. The problem here is that I cannot refer to the RefCell of the node which call the .append() method in the append method itself (because self is a DoubleLinkedList instance). I cannot pass the previous element instance as well because I would need a second mutable reference of self which is not possible to have. I'm confused and i'm starting to think that I chose the wrong structures.

markdown for code please Markdown Cheatsheet · adam-p/markdown-here Wiki · GitHub .

does this help? Simple doubly-linked list in safe Rust · GitHub

// ...
            if let Some(ref mut next) = node.borrow_mut().next {
                Self::append(next, data)
            } else { None }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.