You need "let (a, _) = *self" or "let &(a, _) = self". self is a pointer to a tuple, not a tuple.

Niko

Alexander Stavonin wrote:
Thank you! But I still can not compile it:


   1 pub trait TupleVal<T>  {
   2     pub pure fn _1(&self) ->  &self/T;
   3     pub pure fn _2(&self) ->  &self/T;
   4 }
   5
   6 impl<T>(T, T): TupleVal<T>  {
   7     pure fn _1(&self) ->  &self/T {
   8         let (a, _) = self;
   9         a
  10     }
  11     pure fn _2(&self) ->  &self/T {
  12         let (_, b) = self;
  13         b
  14     }
  15 }

test.rs:8:12: 8:18 error: mismatched types: expected `&self/('a,'a)`, found 
tuple
test.rs:8         let (a, _) = self;
                       ^~~~~~


On Feb 1, 2013, at 11:38 PM, Niko Matsakis<n...@alum.mit.edu>  wrote:

What you want is something like this (and corresponding changes to the impl):

pub trait TupleVal<T>  {
     pub pure fn _1(&self) ->  &self/T;
     pub pure fn _2(&self) ->  &self/T;
}

The `&self` declaration is called an "explicit self" declaration.  What you have currently written is called 
"implicit self" and is deprecated.  Explicit self also tells the compiler what sort of pointer you expect: in this 
case, a borrowed pointer to the receiver.  The lifetime of this borrowed pointer is always called "self".  Therefore, 
the return type `&self/T` says: "a pointer with the same lifetime as the receiver to T".

Note that this syntax is likely to change in the future, although the precise 
form is not yet finalized.  I suspect it will be something like:

pub trait TupleVal<T>  {
     pub pure fn _1(&'v self) ->  &'v T;
     pub pure fn _2(&'v self) ->  &'v T;
}

which makes the connection between the lifetime of the self pointer and the 
lifetime of the return value more explicit.


Niko

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to