I'm trying to implement a generic Cons List, one somewhat more advanced than the one used in chapter 15 of the book:
use std::fmt::Debug;
#[derive(Debug)]
enum List<T> {
Nil,
Cons(T, Box<List<T>>),
}
impl<T> List<T>
where
T: Debug,
{
fn from_iterable(iterator: &Iterator<Item = T>) -> Self {
iterator.fold(List::Nil, |acc, value| List::Cons(value, Box::new(acc)))
}
}
fn main() {
println!("{:?}", List::from_iterable(&(1..10)));
}
My code does not compile and it has a really confusing message:
error: the `fold` method cannot be invoked on a trait object
--> src/main.rs:14:18
|
14 | iterator.fold(List::Nil, |acc, value| List::Cons(value, Box::new(acc)))
| ^^^^
What does this message mean?
I have seen this somehow related question, but even if this one is a duplicate my current knowledge is too limited to connect the dots.