Reversal of a singly-linked list in Rust I was trying to write some simple data structures to practice ownership concepts in Rust:

type Link&lt;T&gt; = Option&lt;Box&lt;Node&lt;T&gt;&gt;&gt;;

struct Node&lt;T&gt; {
    elem: T,
    next: Link&lt;T&gt;,
}

struct List&lt;T&gt; {
    head: Link&lt;T&gt;,
}

pub struct Iter&lt;'a, T: 'a&gt; {
    next: Option&lt;&amp;'a Node&lt;T&gt;&gt;,
}

impl&lt;T&gt; List&lt;T&gt; {
    pub fn new() -&gt; Self {
        List { head: None }
    }

    pub fn push_front(&amp;mut self, elem: T) {
        let new_node = Box::new(Node {
            elem: elem,
            next: self.head.take(),
        });

        self.head = Some(new_node);
    }

    pub fn reverse(&amp;mut self) {
        if self.head.is_none() || self.head.as_ref().unwrap().next.is_none() {
            return;
        }

        let mut prev = None;
        let mut current_node = self.head.take();
        while current_node.is_some() {
            let next = current_node.as_mut().unwrap().next.take();
            current_node.as_mut().unwrap().next = prev.take();
            prev = current_node.take();
            current_node = next;
        }

        self.head = prev.take();
    }

    pub fn iter(&amp;self) -&gt; Iter&lt;T&gt; {
        Iter { next: self.head.as_ref().map(|node| &amp;**node) }
    }
}

impl&lt;'a, T&gt; Iterator for Iter&lt;'a, T&gt; {
    type Item = &amp;'a T;

    fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
        self.next.map(|node| {
            self.next = node.next.as_ref().map(|node| &amp;**node);
            &amp;node.elem
        })
    }
}

fn main() {
    let mut l = List::new();
    l.push_front(3);
    l.push_front(2);
    l.push_front(1);
    l.push_front(12);

    for item in l.iter() {
        print!("{} ", item);
    }
    println!();
    l.reverse();
    for item in l.iter() {
        print!("{} ", item);
    }
    println!();
}


Link to Rust Playground
Before I begin, I recommend reading Learning Rust With Entirely Too Many Linked Lists.


Avoid unwraps, especially those after checking is_some. Use pattern matching instead.
The first test in reverse is redundant with the rest of the logic.
Implement Debug or Display to DRY up the printing of the list.
You should have some tests, especially for edge cases: 0, 1, and 2 items.
I'm not a fan of side effects inside of a map - it should err more on the side of purely functional. I'd use a match, even though it's more lines of code.
I'd simplify the iterator by just keeping a &amp;Link
You could choose to remove the temporary variable from push_front.
for item in foo.iter() is usually written as for item in &amp;foo, which implies that you need to implement IntoIterator for a reference to your type.




use std::fmt;

type Link&lt;T&gt; = Option&lt;Box&lt;Node&lt;T&gt;&gt;&gt;;

struct Node&lt;T&gt; {
    elem: T,
    next: Link&lt;T&gt;,
}

struct List&lt;T&gt; {
    head: Link&lt;T&gt;,
}

pub struct Iter&lt;'a, T: 'a&gt; {
    next: &amp;'a Link&lt;T&gt;,
}

impl&lt;T&gt; List&lt;T&gt; {
    pub fn new() -&gt; Self {
        List { head: None }
    }

    pub fn push_front(&amp;mut self, elem: T) {
        self.head = Some(Box::new(Node {
            elem: elem,
            next: self.head.take(),
        }));
    }

    pub fn reverse(&amp;mut self) {
        let mut prev = None;
        let mut current_node = self.head.take();

        while let Some(mut current_node_inner) = current_node.take() {
            let next = current_node_inner.next.take();
            current_node_inner.next = prev.take();
            prev = Some(current_node_inner);
            current_node = next;
        }

        self.head = prev.take();
    }

    pub fn iter(&amp;self) -&gt; Iter&lt;T&gt; {
        Iter { next: &amp;self.head }
    }
}

impl&lt;'a, T&gt; IntoIterator for &amp;'a List&lt;T&gt; {
    type Item = &amp;'a T;
    type IntoIter = Iter&lt;'a, T&gt;;

    fn into_iter(self) -&gt; Self::IntoIter {
        self.iter()
    }
}

impl&lt;'a, T&gt; Iterator for Iter&lt;'a, T&gt; {
    type Item = &amp;'a T;

    fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
        match self.next.as_ref() {
            Some(next) =&gt; {
                self.next = &amp;next.next;
                Some(&amp;next.elem)
            }
            None =&gt; None,
        }
    }
}

impl&lt;T&gt; fmt::Debug for List&lt;T&gt;
    where T: fmt::Debug,
{
    fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
        for item in self {
            write!(f, "{:?} ", item)?;
        }
        Ok(())
    }
}