介绍
视频地址:www.bilibili.com/video/av78062009/
相关源码:github.com/anonymousGiga/Rust-link...
详细内容
本节我们实现双链表的Peek。
实现peek
根据我们在之前实现单链表的经验,我们很容易想到我们的peek函数怎么实现,如下:
pub fn peek_front(&self) -> Option<&T> { | |
self.head.as_ref().map(|node| {&node.borrow().elem | |
})} |
编译,发现会报错。
解决办法,我们可以查看手册上RefCell的borrow函数,发现定义如下:
pub fn borrow(&self) -> Ref<'_, T> | |
pub fn borrow_mut(&self) -> RefMut<'_, T> |
那我们将代码改为如下:
pub fn peek_front(&self) -> Option<Ref<T>> { | |
self.head.as_ref().map(|node| { | |
node.borrow()})} |
编译,发现仍然报错,找不到Ref,则在代码中添加如下:
use std::cell::Ref;
继续报错,报错情况如下:
= note: expected enum `std::option::Option<std::cell::Ref<'_, T>>` | |
found enum `std::option::Option<std::cell::Ref<'_, Node<T>>>` |
期望的是
std::option::Option<std::cell::Ref<'_, T>>
但是此处得到的是
std::option::Option<std::cell::Ref<'_, Node<T>>>
很容易发现,我们需要将
Node<T>
中的T拿出来。
此时该如何解决呢?
这里我们就使用到
std::cell::ref
中的map,如下:
pub fn map<U, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, U> | |
where | |
F: FnOnce(&T) -> &U,U: ?Sized, |
最终,我们的peek的代码如下:
pub fn peek_front(&self) -> Option<Ref<T>> { | |
self.head.as_ref().map(|node| { | |
Ref::map(node.borrow(), |node| &node.elem)})} |
测试代码
fn peek() {let mut list = List::new(); | |
assert!(list.peek_front().is_none()); | |
list.push_front(1); list.push_front(2); list.push_front(3); | |
assert_eq!(&*list.peek_front().unwrap(), &3); | |
} |
完整代码
use std::rc::Rc; | |
use std::cell::{Ref, RefCell}; | |
pub struct List<T> { | |
head: Link<T>, | |
tail: Link<T>, | |
} | |
type Link<T> = Option<Rc<RefCell<Node<T>>>>; | |
struct Node<T> { | |
elem: T, | |
next: Link<T>, | |
prev: Link<T>, | |
} | |
impl<T> Node<T> { | |
fn new(elem: T) -> Rc<RefCell<Self>> { | |
Rc::new(RefCell::new(Node { | |
elem: elem, | |
prev: None, | |
next: None,}))} | |
} | |
impl<T> List<T> { | |
pub fn new() -> Self { | |
List { head: None, tail: None }} | |
pub fn push_front(&mut self, elem: T) {let node = Node::new(elem); | |
match self.head.take() {Some(head) => {//取可变引用使用borrow_mut | |
head.borrow_mut().prev = Some(node.clone()); | |
node.borrow_mut().next = Some(head); | |
self.head = Some(node);}None => { | |
self.tail = Some(node.clone()); | |
self.head = Some(node);}}} | |
pub fn pop_front(&mut self) -> Option<T> { | |
self.head.take().map(|node| { | |
match node.borrow_mut().next.take() {Some(next) => { | |
next.borrow_mut().prev.take(); | |
self.head = Some(next);}None => { | |
self.tail.take();}}//取值使用into_inner | |
Rc::try_unwrap(node).ok().unwrap().into_inner().elem | |
})} | |
//pub fn peek_front(&self) -> Option<&T> { | |
pub fn peek_front(&self) -> Option<Ref<T>> { | |
self.head.as_ref().map(|node| {//node.borrow() | |
Ref::map(node.borrow(), |node| &node.elem)})} | |
} | |
mod tests { | |
use super::List; | |
fn basics() {let mut list = List::new(); | |
assert_eq!(list.pop_front(), None); | |
list.push_front(1); | |
list.push_front(2); | |
list.push_front(3); | |
assert_eq!(list.pop_front(), Some(3)); | |
assert_eq!(list.pop_front(), Some(2)); | |
list.push_front(4); | |
list.push_front(5); | |
assert_eq!(list.pop_front(), Some(5)); | |
assert_eq!(list.pop_front(), Some(4)); | |
assert_eq!(list.pop_front(), Some(1)); | |
assert_eq!(list.pop_front(), None);} | |
} |