I have a linked-list sort of struct, using Option, Rc, and RefCell.
I'd like to implement fmt::Debug for it, but have run into the lovely "cannot move out of borrowed content" error.
use std::fmt;
use std::rc::{Rc, Weak};
use std::cell::RefCell;
#[derive(Clone, Debug, Ord, Eq, PartialOrd, PartialEq)]
struct NodeId {id: String}
impl NodeId {
pub fn new(s: &str) -> NodeId { NodeId{id: s.to_string()}}
}
struct NodeInfo {
nodeid: NodeId,
prev: Option<Rc<RefCell<NodeInfo>>>,
next: Option<Rc<RefCell<NodeInfo>>>,
}
impl fmt::Debug for NodeInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NodeInfo( {} {} {} )", self.nodeid.id,
match self.prev { None => "none".to_string(), Some(ref n) => n.borrow().nodeid.id},
match self.next { None => "none".to_string(), Some(ref n) => "some".to_string()},
)
}
}
fn main() {}
Ideally the debug output would be able to show me the ids of the .next and .previous nodes. But Rust is not allowing access to them. The attempt to .borrow() the content of the RefCell causes the error, but I cannot understand why.
Play with it here: http://is.gd/Sah7sT