Skip to content

Commit

Permalink
simple-linked-list: rev consumes self, eliminating Clone bound
Browse files Browse the repository at this point in the history
  • Loading branch information
coriolinus committed Oct 10, 2019
1 parent 98772ab commit a2e3121
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
11 changes: 4 additions & 7 deletions exercises/simple-linked-list/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ impl<T> SimpleLinkedList<T> {
pub fn peek(&self) -> Option<&T> {
self.head.as_ref().map(|node| &node.data)
}
}

impl<T: Clone> SimpleLinkedList<T> {
pub fn rev(&self) -> SimpleLinkedList<T> {
pub fn rev(self) -> SimpleLinkedList<T> {
let mut rev_list = SimpleLinkedList::new();
let mut next = self.head.as_ref().map(|node| &**node);
while let Some(node) = next {
rev_list.push(node.data.clone());
next = node.next.as_ref().map(|node| &**node);
let mut vec: Vec<_> = self.into();
for t in vec.drain(..).rev() {
rev_list.push(t);
}
rev_list
}
Expand Down
4 changes: 1 addition & 3 deletions exercises/simple-linked-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ impl<T> SimpleLinkedList<T> {
pub fn peek(&self) -> Option<&T> {
unimplemented!()
}
}

impl<T: Clone> SimpleLinkedList<T> {
pub fn rev(&self) -> SimpleLinkedList<T> {
pub fn rev(self) -> SimpleLinkedList<T> {
unimplemented!()
}
}
Expand Down

0 comments on commit a2e3121

Please sign in to comment.