Hi all,

I posted this question on [Stack Overflow][1], but it is yet to be
answered there, so I ask here too.

Why reference to traits are not cloneable? The following code compiles:

struct Test;

fn clone_vec<'a>(v: Vec<&'a Test>) -> Vec<&'a Test> {
    v.clone()
}

But this one doesn't:

trait Test {
    fn do_smt(&self);
}

fn clone_vec<'a>(v: Vec<&'a Test>) -> Vec<&'a Test> {
    v.clone()
}

It spits this error:

main3.rs:7:5: 7:14 error: failed to find an implementation of trait
std::clone::Clone for &'a Test<no-bounds>
main3.rs:7     v.clone()
               ^~~~~~~~~

However, Clone trait is implemented for all kinds of references, as
far as I can see. I just don't see why &'a Test where Test is a trait
is not cloneable.

This is very strange provided that I can implement clone_vec() myself,
without using Clone:

fn clone_vec<'a>(v: Vec<&'a Test>) -> Vec<&'a Test> {
    let mut result = Vec::new();
    for &e in v.iter() {
        result.push(e);
    }
    result
}

It works just fine.

If this is a bug, I would gladly submit it to the tracker, but I'm not
sure that I'm not doing something wrong.

    [1]: 
http://stackoverflow.com/questions/23426545/references-to-trait-objects-are-not-cloneable
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to