Le 21 avr. 2014 à 12:57, Artella Coding <[email protected]> a écrit 
:

> 
> Suppose I have the enum : 
> 
> enum Coll<T> {
>     A(f64,T),
>     B(f64,T)
> }
> 
> I know that the compiler can automatically derive the Clone trait for the 
> above. However I would like to manually implement it and was unsuccessful in 
> my attempt. I tried the following : 
> 
> enum Coll<T> {
>     A(f64,T),
>     B(f64,T)
> }
> 
> impl<T: Clone> Clone for Coll<T> {
>     fn clone (&self) -> Coll<T> {
>         match *self {
>             A(x,y) => A(x,y.clone()),
>             B(x,y) => B(x,y.clone())
>         }
>     }
> }

The error is in your match statement. You try to bind the "T" part of your type 
to the "y" variable, which causes by default a by-value binding.
Since you can't know statically if T has value or move semantics, you can't do 
this, since in the case of move semantics, this would move the T out of self, 
that you don't own because you take it as a reference (&self).

You have instead to explicitly bind T by ref :

        match *self {
            A(x, ref y) => A(x ,y.clone()),
            B(x, ref y) => B(x, y.clone())
        }

Leo

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to