Sorry, I told you wrong. When you write:
let &(a, _) = self;
&a
You are actually copying the value out of `self` and into the stack
variable `a`. The error message is telling you that you are returning a
pointer into your stack frame, which is of course unsafe.
What you actually want is this:
let &(ref a, _) = self;
a
The `ref` keyword indicates that the `a` variable should be a pointer
into the value being matched (here, `self`) and not copied out.
Niko
Alexander Stavonin wrote:
I made changes as you told me:
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 }
16
17 fn main() {
18 let pair = (1, 2);
19 let left = *pair._1();
20 io::println(fmt!("pair left: %u", left));
21 /*let r = pair._2();*/
22 }
But:
test.rs:9:9: 9:10 error: illegal borrow: borrowed value does not live long
enough
test.rs:9&a
^
test.rs:7:33: 10:5 note: borrowed pointer must be valid for the lifetime&self
as defined on the block at 7:33...
test.rs:7 pure fn _1(&self) -> &self/T {
test.rs:8 let&(a, _) = self;
test.rs:9&a
test.rs:10 }
test.rs:7:33: 10:5 note: ...but borrowed value is only valid for the block at
7:33
test.rs:7 pure fn _1(&self) -> &self/T {
test.rs:8 let&(a, _) = self;
test.rs:9&a
test.rs:10 }
error: aborting due to previous error
Did I miss something?
On Feb 2, 2013, at 9:08 AM, Niko Matsakis<[email protected]> wrote:
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;
^~~~~~
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev