Browse Source

First list done

master
Lachlan Jacob 5 years ago
commit
b474cd2b97
6 changed files with 93 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 0
    0
      NOTES.md
  3. 6
    0
      lists/Cargo.lock
  4. 9
    0
      lists/Cargo.toml
  5. 76
    0
      lists/src/first.rs
  6. 1
    0
      lists/src/lib.rs

+ 1
- 0
.gitignore View File

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

+ 0
- 0
NOTES.md View File


+ 6
- 0
lists/Cargo.lock View File

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


+ 9
- 0
lists/Cargo.toml View File

@@ -0,0 +1,9 @@
[package]
name = "lists"
version = "0.1.0"
authors = ["Lachlan Jacob <lj343@icloud.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

+ 76
- 0
lists/src/first.rs View File

@@ -0,0 +1,76 @@
use std::mem;

pub struct List {
head: Link
}

enum Link {
Empty,
More(Box<Node>),
}

struct Node {
elem: i32,
next: Link,
}

impl List {
pub fn new() -> Self {
List { head: Link::Empty }
}

pub fn push(&mut self, elem: i32) {
let new_node = Box::new(Node {
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 Drop for List {
fn drop(&mut self) {
let mut cur_link = mem::replace(&mut self.head, Link::Empty);
while let Link::More(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.ndex, Link::Empty);
}
}
}

#[cfg(test)]
mod test {

use super::List;

#[test]
fn basics() {
let mut list = List::new();

assert_eq!(list.pop(), None);

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

assert_eq!(list.pop(), Some(3));
assert_eq!(list.pop(), Some(2));

list.push(4);
list.push(5);

assert_eq!(list.pop(), Some(5));
assert_eq!(list.pop(), Some(4));

assert_eq!(list.pop(), Some(1));
assert_eq!(list.pop(), None);
}
}

+ 1
- 0
lists/src/lib.rs View File

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

Loading…
Cancel
Save