Recommendations for immutable linked list type

Basically, I want a list type that has a cheap, non-destructive prepend operation (i.e., cons). The standard library's LinkedList type seems to be destructive.

How about the im crate? Otherwise you can do it yourself with Rc.

I thought of that, but the im crate is unmaintained IIRC (and its fork doesn't have much usage).

How would you suggest going about it with Rc? Would it be something like this?

enum List<T> {
  Nil,
  Cons(T, Rc<List<T>>),
}

Sounds like you might be looking exactly for A Persistent Stack - Learning Rust With Entirely Too Many Linked Lists

1 Like

Thanks! I'll give that a try. It would be nice to have a well-used library for stuff like this, but alas….

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.