I am trying to implement a Debug
for my custom List<T>
using a while
loop but stuck at taking a reference of a pattern matched RefCell.
use std::rc::Rc;
use std::cell::RefCell;
type Link<T> = Option<Rc<RefCell<Node<T>>>>;
struct Node<T> {val: T, next: Link<T>}
struct List<T> {head: Link<T>, tail: Link<T>, len: usize}
use std::fmt;
impl<T: fmt::Debug> fmt::Debug for List<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut temp = &self.head;
while let Some(r) = temp {
write!(f, "{:?} -> ", r.borrow().val);
temp = &r.borrow().next; // ERROR
// temp = unsafe {& (*r.as_ptr()).next };
}
write!(f, "[]")
}
}
Is there an Elegant, Idiomatic & Safe Rust that can get around this problem?
I don't want to implement Iterator for my list.