I've hit a weird snag. The following code fails to compile:
trait Foo: Clone {
fn bar(&self) -> usize;
}
struct Baz {
bits: Vec<Box<Foo>>,
}
impl Baz {
fn bunk(&self) -> usize {
let mut ans = 0;
for b in &self.bits {
ans = ans + b.bar();
}
ans
}
}
fn main() {
}
with an error about Foo not implementing either Clone or Foo, but if I remove the requirement that Foo extends Clone, then it compiles just fine.
Ultimately, I'd like a Vec of objects each of which implements Foo, but which are not necessarily all the same type. I'd then like to call the bar method on each of these objects (which I know must be implemented because those objects implement Foo, even though I understand that Foo itself does not implement Foo). Is there some way I can actually do this without using tuples (since I don't know at compile-time how many objects which implement Foo I'll have)?