pubfnpop_front(&mut self) ->Option<T> {// need to take the old head, ensuring it's -2 self.head.take().map(|old_head| { // -1 oldmatch old_head.borrow_mut().next.take() {Some(new_head) => { // -1 new// not emptying list new_head.borrow_mut().prev.take(); // -1 old self.head =Some(new_head); // +1 new// total: -2 old, +0 new }None=> {// emptying list self.tail.take(); // -1 old// total: -2 old, (no new) } } old_head.elem })}
> cargo builderror[E0609]: no field `elem` on type `std::rc::Rc<std::cell::RefCell<fourth::Node<T>>>`--> src/fourth.rs:64:22|64| old_head.elem|^^^^ unknown field
> cargo builderror[E0599]: no method named `unwrap` found for type `std::result::Result<std::cell::RefCell<fourth::Node<T>>, std::rc::Rc<std::cell::RefCell<fourth::Node<T>>>>` in the current scope
--> src/fourth.rs:64:38|64|Rc::try_unwrap(old_head).unwrap().into_inner().elem|^^^^^^|= note: the method `unwrap` exists but the following trait bounds were not satisfied: `std::rc::Rc<std::cell::RefCell<fourth::Node<T>>> : std::fmt::Debug`
对 Result 进行解包要求可以 Debug 打印错误情况。 RefCell 仅在 T 执行时才实现Debug。 Node 未实现Debug。
#[cfg(test)]mod test {use super::List; #[test]fnbasics() {letmut list =List::new();// Check empty list behaves rightassert_eq!(list.pop_front(), None);// Populate list list.push_front(1); list.push_front(2); list.push_front(3);// Check normal removalassert_eq!(list.pop_front(), Some(3));assert_eq!(list.pop_front(), Some(2));// Push some more just to make sure nothing's corrupted list.push_front(4); list.push_front(5);// Check normal removalassert_eq!(list.pop_front(), Some(5));assert_eq!(list.pop_front(), Some(4));// Check exhaustionassert_eq!(list.pop_front(), Some(1));assert_eq!(list.pop_front(), None); }}