runningwater 2 år sedan
incheckning
19286b6878
5 ändrade filer med 82 tillägg och 0 borttagningar
  1. 1 0
      .gitignore
  2. 7 0
      Cargo.lock
  3. 8 0
      Cargo.toml
  4. 65 0
      src/first.rs
  5. 1 0
      src/lib.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/target

+ 7 - 0
Cargo.lock

@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "lists"
+version = "0.1.0"

+ 8 - 0
Cargo.toml

@@ -0,0 +1,8 @@
+[package]
+name = "lists"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]

+ 65 - 0
src/first.rs

@@ -0,0 +1,65 @@
+use std::mem;
+
+/// `List` 列表
+pub struct List {
+    head: Link,
+}
+struct Node {
+    elem: i32,
+    next: Link,
+}
+enum Link {
+    Empty,
+    More(Box<Node>),
+}
+
+impl List {
+    pub fn new() -> Self {
+        List { head: Link::Empty }
+    }
+    pub fn push(&mut self, elem: i32) {
+        let new_node = Box::new(Node {
+            elem: elem,
+            next: mem::replace(&mut self.head, Link::Empty),
+        });
+
+        self.head = Link::More(new_node);
+    }
+    pub fn pop(&mut self) -> Option<i32> {
+        match mem::replace(&mut self.head, Link::Empty) {
+            Link::Empty => None,
+            Link::More(node) => {
+                self.head = node.next;
+                Some(node.elem)
+            }
+        }
+    }
+}
+impl Default for List {
+    fn default() -> Self {
+        Self { head: Link::Empty }
+    }
+}
+
+mod test {
+    use crate::first;
+
+    #[test]
+    fn basics() {
+        let mut list = first::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(3));
+        assert_eq!(list.pop(), Some(2));
+        
+    }
+}

+ 1 - 0
src/lib.rs

@@ -0,0 +1 @@
+pub mod first;