This would be nice addition. However, can the methods start at zero instead of one?
On Fri, Feb 1, 2013 at 6:38 AM, Niko Matsakis <[email protected]> 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 > > > Alexander Stavonin wrote: > > I want to add function like _1(), _2(), etc for Rust tuple. Unfortunately > I do not understand how to tell compiler lifetime of returning result in > case of `trait` > > pub trait TupleVal<T> { > pub pure fn _1() -> T; > pub pure fn _2() -> T; > } > > impl <T>(T, T): TupleVal<T> { > pure fn _1() -> T { > let (a, _) = self; > a > } > pure fn _2() -> T { > let (_, b) = self; > b > } > } > > And the errors: > > test.rs:31:21: 31:25 error: moving out of self reference > test.rs:31 let (a, _) = self; > ^~~~ > test.rs:35:21: 35:25 error: moving out of self reference > test.rs:35 let (_, b) = self; > ^~~~ > error: aborting due to 2 previous errors > > How can I tell the compiler returning values lifetime? Actually it > couldn't be more than lifetime of self. > > _______________________________________________ > Rust-dev mailing > [email protected]https://mail.mozilla.org/listinfo/rust-dev > > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
