good to know. should I be reporting the bug to some sort of bug-report-system?
Rémi On Mon, Jun 24, 2013 at 11:48 PM, Philipp Brüschweiler <[email protected]>wrote: > Hi Rémi, > > Yes, this looks like a compiler bug to me. > > Cheers, > Philipp > > > On Mon, Jun 24, 2013 at 1:07 PM, Rémi Fontan <[email protected]> wrote: > >> I have another example that puzzles me. >> >> struct Vec { x:float, y:float, z:float } >> impl Vec { >> pub fn getRef<'a>(&'a mut self, i:uint) -> &'a mut float { >> // if(i==0) { &mut self.x } >> // else if(i==1) { &mut self.x } >> // else {&mut self.x } >> >> match(i) { >> 0 => &mut self.x, >> 1 => &mut self.y, >> _ => &mut self.z >> } >> } >> } >> >> when compiling with match I get following errors : >> >> rustc test.rs -o test-test --test >> test.rs:122:17: 122:28 error: cannot infer an appropriate lifetime due >> to conflicting requirements >> test.rs:122 _ => &mut self.z >> ^~~~~~~~~~~ >> test.rs:121:17: 121:28 note: first, the lifetime must be contained by >> the expression at 121:17... >> test.rs:121 1 => &mut self.y, >> ^~~~~~~~~~~ >> test.rs:121:17: 121:28 note: ...due to the following expression >> test.rs:121 1 => &mut self.y, >> ^~~~~~~~~~~ >> test.rs:120:17: 120:28 note: but, the lifetime must also be contained by >> the expression at 120:17... >> test.rs:120 0 => &mut self.x, >> ^~~~~~~~~~~ >> test.rs:120:17: 120:28 note: ...due to the following expression >> test.rs:120 0 => &mut self.x, >> ^~~~~~~~~~~ >> error: aborting due to previous error >> make: *** [test-test] Error 101 >> >> >> >> but it compiles correctly with the if version. I don't understand why >> it's not behaving the same way. Actually I don't understand why the >> compiler is not capable of finding out about the life cycle on its own for >> this particular example. Could the life cycle of the return type of getRef >> be inferred from the lifecycle of the intersection of {self.x, self.y, >> self.z} ? >> >> cheers, >> >> Rémi >> >> >> >> >> On Mon, Jun 24, 2013 at 2:32 AM, Philipp Brüschweiler >> <[email protected]>wrote: >> >>> Hi Rémi, >>> >>> Yes, the documentation of Rust is not very good at the moment. The >>> concept and syntax of lifetimes is explained in this and the following >>> chapter: >>> >>> >>> http://static.rust-lang.org/doc/tutorial-borrowed-ptr.html#returning-borrowed-pointers >>> >>> The only single quotes are only used to declare literal characters, e.g. >>> 'c', and lifetime variables. >>> >>> Cheers, >>> Philipp >>> >>> >>> On Sun, Jun 23, 2013 at 12:03 PM, Rémi Fontan <[email protected]>wrote: >>> >>>> thanks, it works. >>>> >>>> however it is not yet very clear to me how lifetime works in rust. is >>>> there a bit of doc that explain about the concept of lifetime and the >>>> syntax? >>>> >>>> is using a quote before the lifetime variable necessary? I realised >>>> that the first time I read the rust tutorial I saw a lot of variables with >>>> a quote in front of their name, I simply ignore that details and went >>>> ahead. Were there all lifetime variables? >>>> >>>> cheers, >>>> >>>> Rémi >>>> >>>> >>>> On Sat, Jun 22, 2013 at 8:47 PM, Philipp Brüschweiler <[email protected] >>>> > wrote: >>>> >>>>> Hi Rémi, >>>>> >>>>> The problem in your code was that you have to return a &mut reference. >>>>> Here's a version of your code that works: >>>>> >>>>> ``` >>>>> >>>>> struct Mat { >>>>> data:[float, ..16] >>>>> } >>>>> >>>>> impl Mat { >>>>> pub fn new() -> Mat { >>>>> Mat { data: [0.0, ..16] } >>>>> } >>>>> pub fn Get<'a>(&'a mut self, r:int, c:int) -> &'a mut float { >>>>> &mut self.data[r+c*4] >>>>> } >>>>> } >>>>> >>>>> fn main() { >>>>> let mut a = Mat::new(); >>>>> *a.Get(0, 0) = 5.0; >>>>> println(fmt!("%?", a)); >>>>> } >>>>> ``` >>>>> >>>>> Note that besides taking an &mut reference, we have to tell the >>>>> compiler that the lifetime of that reference is the same as of the >>>>> original >>>>> object, which is done by anotating both references with the same lifetime >>>>> variable ('a in this code). >>>>> >>>>> Cheers, >>>>> Philipp >>>>> >>>>> >>>>> On Sat, Jun 22, 2013 at 10:35 AM, Rémi Fontan <[email protected]>wrote: >>>>> >>>>>> Hi, >>>>>> >>>>>> is there a way to write something as follow in rust? >>>>>> >>>>>> class matrix { >>>>>> ... >>>>>> float& Get(int r, int c) { return this.data[r + c*4]; } >>>>>> } >>>>>> >>>>>> matrix m; >>>>>> m.Get(0, 1) = 42.; >>>>>> >>>>>> >>>>>> in trust, my struct matrix would have a method called Get, but what >>>>>> would it return? >>>>>> >>>>>> struct Mat { >>>>>> data:[float, ..16] >>>>>> } >>>>>> >>>>>> impl Mat { >>>>>> pub fn Get(&mut self, r:int, c:int) -> &mut float { >>>>>> &self.data[r+c*4] >>>>>> } >>>>>> } >>>>>> >>>>>> >>>>>> I get error messages about lifetime or about mutability >>>>>> >>>>>> est.rs:107:8: 107:24 error: mismatched types: expected `&mut float` >>>>>> but found `&float` (values differ in mutability) >>>>>> test.rs:107 &self.data[r*4+c] >>>>>> ^~~~~~~~~~~~~~~~ >>>>>> >>>>>> >>>>>> cheers, >>>>>> >>>>>> Remi >>>>>> -- >>>>>> Rémi Fontan : [email protected] >>>>>> mobile: +64 21 855 351 >>>>>> 93 Otaki Street, Miramar 6022 >>>>>> Wellington, New Zealand >>>>>> >>>>>> _______________________________________________ >>>>>> Rust-dev mailing list >>>>>> [email protected] >>>>>> https://mail.mozilla.org/listinfo/rust-dev >>>>>> >>>>>> >>>>> >>>> >>>> >>>> -- >>>> Rémi Fontan : [email protected] >>>> mobile: +64 21 855 351 >>>> 93 Otaki Street, Miramar 6022 >>>> Wellington, New Zealand >>>> >>> >>> >> >> >> -- >> Rémi Fontan : [email protected] >> mobile: +64 21 855 351 >> 93 Otaki Street, Miramar 6022 >> Wellington, New Zealand >> > > -- Rémi Fontan : [email protected] mobile: +64 21 855 351 93 Otaki Street, Miramar 6022 Wellington, New Zealand
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
