rust_examples 0.1.11

rust 的学习中的一些例子
Documentation
struct Node<T> {
    element: T,
    next: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
struct List<T> {
    head: Link<T>,
    tail: *mut Node<T>,
}
impl<T> List<T> {
    pub fn new() -> Self {
        List {
            head: None,
            tail: std::ptr::null_mut(),
        }
    }
    pub fn push(&mut self, element: T) {
        let mut new_tail = Box::new(Node {
            element,
            next: None,
        });
        // new_tail 必须为Box。否则所有权会被转移
        let raw_new_tail: *mut Node<T> = &mut *new_tail;
        if self.tail.is_null() {
            self.head = Some(new_tail);
        } else {
            unsafe {
                let node = &mut *self.tail;
                node.next = Some(new_tail);
            }
        }
        self.tail = raw_new_tail;
    }
    pub fn pop(&mut self) -> Option<T> {
        self.head.take().map(|old_head| {
            self.head = old_head.next;
            if self.head.is_none() {
                self.tail = std::ptr::null_mut();
            }
            old_head.element
        })
    }
}
#[cfg(test)]
mod test {
    use super::List;
    #[test]
    fn basics() {
        let mut list = List::new();

        // Check empty list behaves right
        assert_eq!(list.pop(), None);

        // Populate list
        list.push(1);
        list.push(2);
        list.push(3);

        // Check normal removal
        assert_eq!(list.pop(), Some(1));
        assert_eq!(list.pop(), Some(2));

        // Push some more just to make sure nothing's corrupted
        list.push(4);
        list.push(5);

        // Check normal removal
        assert_eq!(list.pop(), Some(3));
        assert_eq!(list.pop(), Some(4));

        // Check exhaustion
        assert_eq!(list.pop(), Some(5));
        assert_eq!(list.pop(), None);

        // Check the exhaustion case fixed the pointer right
        list.push(6);
        list.push(7);

        // Check normal removal
        assert_eq!(list.pop(), Some(6));
        assert_eq!(list.pop(), Some(7));
        assert_eq!(list.pop(), None);
    }
}