1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
prelude!();
#[test]
fn test_linked_list() {
let out: Vec<i64> = rune! {
// An empty placeholder in a node.
struct Empty;
// A single node in the linked list.
struct Node {
value,
next,
}
// The linked list.
struct List {
first,
last,
}
impl List {
// Construct a new linked list.
fn new() {
List {
first: Empty,
last: Empty,
}
}
// Construct an iterator over the linked list.
fn iter(self) {
Iter {
current: self.first,
}
}
// Push an element to the back of the linked list.
fn push_back(self, value) {
let prev = self.last;
self.last = Node {
value,
next: Empty,
};
if prev is Empty {
self.first = self.last;
} else {
prev.next = self.last;
}
}
}
struct Iter {
current,
}
impl Iter {
// Iterate over the next element.
fn next(self) {
if self.current is Empty {
return None;
}
let value = self.current;
self.current = value.next;
Some(value.value)
}
}
let ll = List::new();
ll.push_back(1);
ll.push_back(2);
ll.push_back(3);
let it = ll.iter();
let out = [];
while let Some(value) = Iter::next(it) {
out.push(value);
}
out
};
assert_eq!(out, vec![1, 2, 3]);
}