[rust-dev] Cloning Generic Structs/Enums with lifetime parameters

Artella Coding artella.coding at googlemail.com
Mon Apr 21 09:30:43 PDT 2014


Cool thanks. I had skimmed through the sections you cited, but the problem
was that in my version I was trying to clone by creating a new copy of x as
follows :

impl<'a,T: Clone> Clone for Cls<'a,T> {
    fn clone(&self) -> Cls<'a, T> {
        Cls{ x: & (*self.x).clone() }
    }
}

and I was getting the error message :

test1.rs:8:17: 8:36 error: borrowed value does not live long enough
test1.rs:8 Cls{ x: & (*self.x).clone() }

Your solution solves my problem. Thanks.


On Mon, Apr 21, 2014 at 5:13 PM, Alex Crichton <alex at crichton.co> wrote:

> > How do I manually implement "clone()" for  Structs/Enums with lifetime
> > parameters (e.g. for struct below)?
> >
> > ---------------------------------------------------
> > struct Cls<'a,T> {
> >       x:&'a T
> > }
>
> impl<'a, T: Clone> Clone for Cls<'a, T> {
>     fn clone(&self) -> Cls<'a, T> {
>         Cls { x: self.x }
>     }
> }
>
> You may find the generics [1] and traits [2] sections of the tutorial
> helpful as well!
>
> [1] - http://static.rust-lang.org/doc/master/tutorial.html#generics
> [2] - http://static.rust-lang.org/doc/master/tutorial.html#traits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140421/41f56562/attachment.html>


More information about the Rust-dev mailing list