Skip to content

Commit 1c27c90

Browse files
committed
Refactored list::append() to be based on List<T>
1 parent 65f1993 commit 1c27c90

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/libcollections/list.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@ impl<T:Eq> List<T> {
8686
}
8787
}
8888

89-
/// Appends one list to another
90-
pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
91-
match *list {
92-
Nil => return other,
93-
Cons(ref head, tail) => {
94-
let rest = append(tail, other);
95-
return @Cons((*head).clone(), rest);
96-
}
97-
}
98-
}
99-
10089
impl<T:'static + Clone> List<T> {
10190
/// Create a list from a vector
10291
pub fn from_vec(v: &[T]) -> List<T> {
@@ -105,6 +94,17 @@ impl<T:'static + Clone> List<T> {
10594
_ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail))
10695
}
10796
}
97+
98+
/// Appends one list to another, returning a new list
99+
pub fn append(&self, other: List<T>) -> List<T> {
100+
match other {
101+
Nil => return self.clone(),
102+
_ => match *self {
103+
Nil => return other,
104+
Cons(ref value, tail) => Cons(value.clone(), @tail.append(other))
105+
}
106+
}
107+
}
108108
}
109109

110110
/*
@@ -225,7 +225,7 @@ mod tests {
225225

226226
#[test]
227227
fn test_append() {
228-
assert!(@List::from_vec([1,2,3,4])
229-
== list::append(@List::from_vec([1,2]), @List::from_vec([3,4])));
228+
assert_eq!(List::from_vec([1, 2, 3, 4]),
229+
List::from_vec([1, 2]).append(List::from_vec([3, 4])));
230230
}
231231
}

0 commit comments

Comments
 (0)