나는 옵션 Rc를, 그리고 RefCell을 사용하여 구조체의 연결 목록 정렬이 있습니다.합니까 RefCell :: 내용을 이동 빌려?
나는 그것을 위해 FMT : 디버그를 구현하고 싶습니다,하지만 사랑스러운 "빌린 컨텐츠 밖으로 이동할 수 없습니다"오류로 실행했다.
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() {}
이상적으로 디버그 출력은 .next 및 .previous 노드의 ID를 표시 할 수 있습니다. 그러나 Rust는 그들에게 접근을 허용하지 않습니다. 시도는 RefCell의 내용이 오류가 발생합니다) (.borrow,하지만 그 이유를 이해할 수 없다.
여기에 플레이 : http://is.gd/Sah7sT
감사 :
나는 반복을 피하기 위해 newtype은을 사용하도록 선택했습니다! 이 동작을 일으키는 Strings의 (비) 복사 가능성은 완전히 나를 피했습니다. –