I thought I'd dive into Rust by imeplementing some very simple structure & algorithms, I started with a linked list. Turns out it's not actually that simple. This is my code so far:
enum List<T>
{
Node(T, ~List<T>),
Nil
}
impl<T> List<T>
{
fn new(vector: &[T]) -> List<T> { Nil }
fn add(&mut self, item: T)
{
let tail = self;
loop
{
match *tail
{
Node(_, ~ref next) => tail = next,
Nil => break
}
}
*tail = Node(item, ~Nil);
}
}
This won't compile because next cannot be assigned to tail in the match statement due to incompatible mutability. I know this can easily be done using garbage collected pointers, but that kind of defeats the educational purpose of the exercise: I'd like to know how to do this without Gc or Rc pointers.