Linked list with removal function in Rust I know there are other structures which are better than the linked list, but I want to implement a singly linked list which has a remove function for study.

After struggling for a few hours, I wrote this code.

use std::rc::Rc;
use std::cell::RefCell;

type Link&lt;T&gt; = Option&lt;Rc&lt;RefCell&lt;Node&lt;T&gt;&gt;&gt;&gt;;

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

impl&lt;T&gt; Node&lt;T&gt; {
    fn new(elem: T) -&gt; Rc&lt;RefCell&lt;Self&gt;&gt; {
        Rc::new(RefCell::new(Node {
            elem: elem,
            next: None,
        }))
    }
}

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

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

    pub fn push(&amp;mut self, elem: T) {
        let new_head = Node::new(elem);
        match self.head.take() {
            Some(old_head) =&gt; {
                new_head.borrow_mut().next = Some(old_head);
                self.head = Some(new_head);
            }
            None =&gt; {
                self.head = Some(new_head);
            }
        }
    }

    pub fn remove(&amp;mut self, elem: T) {
        let mut head = self.head.clone().unwrap();
        if head.borrow().elem == elem {
            self.head = head.borrow().next.clone();
            return;
        }

        loop {
            let next = head.borrow().next.clone();
            match next {
                None =&gt; return,
                Some(ref node) =&gt; {
                    if node.borrow().elem == elem {
                        head.borrow_mut().next = node.borrow().next.clone();
                    }
                    head = node.clone();
                }
            }
        }
    }
}


fn main() {
    let mut list = List::new();
    list.push(3);
    list.push(2);
    list.push(1);
    list.remove(2);
    let head = list.head.unwrap().clone();
    assert_eq!(head.borrow().elem, 1);
    let next = head.borrow().next.clone().unwrap();
    assert_eq!(next.borrow().elem, 3);
}


To iterate this list, I implemented a iterator. 

pub struct Iter&lt;T&gt; {
    next: Link&lt;T&gt;
}

impl&lt;T&gt; Iterator for Iter&lt;T&gt; {
    type Item = Rc&lt;RefCell&lt;Node&lt;T&gt;&gt;&gt;;

    fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
        let next = self.next.clone();
        let next_next = next.clone();
        self.next = match next_next {
            Some(node) =&gt; {
                node.borrow().next.clone()
            }
            None =&gt; {
                None
            }
        };
        next
    }
}

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


fn main() {
    let mut list = List::new();
    list.push(3);
    list.push(2);
    list.push(1);
    list.remove(2);

    let mut it = list.iter();
    let next_node = it.next().unwrap();
    assert_eq!(next_node.borrow().elem, 1);
    let next_node = it.next().unwrap();
    assert_eq!(next_node.borrow().elem, 3);
    let next_node = it.next();
    assert_eq!(next_node.is_none(), true);
}


I'm not sure this code is good, because I think


there are too many clone() or borrow() calls.
code is long and not simple. (is using Rc and RefCell good?)


Are there easier or simpler ways?

Even if this code is not so bad, are there weak or dangerous points?
Read Learning Rust With Entirely Too Many Linked Lists.
Your code has compilation errors with Rust 1.18.0.

error[E0446]: private type `Node&lt;T&gt;` in public interface
  --&gt; src/main.rs:82:5
   |
82 |     type Item = Rc&lt;RefCell&lt;Node&lt;T&gt;&gt;&gt;;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

I derive Debug on almost every struct.
push can have all conditionals removed from it because doing
nothing is the same as setting None as the next value.
remove panics when called on a newly-created list.
pushing 1, 1, 1, 1, 2 and then removing 1 results in the list 2-&gt;1-&gt;1 which doesn't seem any flavor of correct. I assume you wanted to remove the first match.
It's way easier to get the code right when it operates on the same level of abstraction. I've created a method that removes on a &amp;mut Link&lt;T&gt; because that's the shared type between List and Link.
Honestly, it's also easier to get this right with recursive code as well.
Doesn't make sense to return the Rc&lt;RefCell&lt;...&gt;&gt; as the iterator type - people care about the values, not the collection.
Can simplify the iterator by just taking the value out of the iterator state and putting a new value in sometimes.




use std::rc::Rc;
use std::cell::RefCell;

type Link&lt;T&gt; = Option&lt;Rc&lt;RefCell&lt;Node&lt;T&gt;&gt;&gt;&gt;;

#[derive(Debug)]
pub struct Node&lt;T&gt; {
    elem: T,
    next: Link&lt;T&gt;,
}

impl&lt;T&gt; Node&lt;T&gt; {
    fn new(elem: T) -&gt; Rc&lt;RefCell&lt;Self&gt;&gt; {
        Rc::new(RefCell::new(Node {
            elem: elem,
            next: None,
        }))
    }
}

#[derive(Debug)]
pub struct List&lt;T&gt; {
    head: Link&lt;T&gt;,
}

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

    pub fn push(&amp;mut self, elem: T) {
        let new_head = Node::new(elem);
        new_head.borrow_mut().next = self.head.take();
        self.head = Some(new_head);
    }

    pub fn remove(&amp;mut self, elem: T) {
        fn remove_inner&lt;T: Eq&gt;(node: &amp;mut Link&lt;T&gt;, elem: T) {
            if let Some(ref mut n) = node.clone() {
                if elem == n.borrow().elem {
                    *node = n.borrow_mut().next.take();
                    return
                }

                remove_inner(&amp;mut n.borrow_mut().next, elem);
            }
        }

        remove_inner(&amp;mut self.head, elem)
    }
}

fn main1() {
    let mut list = List::new();
    list.push(3);
    list.push(2);
    list.push(1);
    list.remove(2);
    let head = list.head.unwrap().clone();
    assert_eq!(head.borrow().elem, 1);
    let next = head.borrow().next.clone().unwrap();
    assert_eq!(next.borrow().elem, 3);

    let mut list = List::new();
    list.remove(2);

    let mut list = List::new();
    list.push(1);
    list.push(1);
    list.push(1);
    list.push(1);
    list.push(2);
    list.remove(1);
    println!("{:?}", list);
}

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

impl&lt;T&gt; Iterator for Iter&lt;T&gt; {
    type Item = Rc&lt;RefCell&lt;Node&lt;T&gt;&gt;&gt;;

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

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

fn main2() {
    let mut list = List::new();
    list.push(3);
    list.push(2);
    list.push(1);
    list.remove(2);

    let mut it = list.iter();
    let next_node = it.next().unwrap();
    assert_eq!(next_node.borrow().elem, 1);
    let next_node = it.next().unwrap();
    assert_eq!(next_node.borrow().elem, 3);
    let next_node = it.next();
    assert_eq!(next_node.is_none(), true);
}

fn main() {
    main1();
    main2();
}


Frankly, I don't know why you used Rc and RefCell — they aren't needed to implement a singly linked list. There's no need for complicated shared ownership (no need for Rc) and there's no need for interior mutability (no need for RefCell).