[rust-dev] Specifying lifetimes in return types of overloaded operators

Artella Coding artella.coding at googlemail.com
Mon Apr 21 10:12:07 PDT 2014


Just realised that the following seems to work :

struct Cls<'a> {
    vec : &'a ~[~int]
}

impl<'a> Index<uint,&'a ~int> for Cls<'a> {
    fn index(&self,i:&uint)->&'a ~int {
        &(self.vec[*i])
    }
}

fn main(){}


On Wed, Apr 16, 2014 at 6:23 AM, Artella Coding <
artella.coding at googlemail.com> wrote:

> Hi Eric, this is an example of code which gives the error :
>
> ************************************************************
> struct Cls {
>       vec : ~[~int]
> }
>
> /*
>     Does not compile, yielding error message :
>
>
>     "method `index` has an incompatible type for trait:
>     expected concrete lifetime, but found bound lifetime
>     parameter &"
> */
> impl<'a> Index<uint,&'a ~int> for Cls {
>       fn index(&'a self, i: &uint) -> &'a ~int {
>           let val = &'a self.vec[*i];
>           return val;
>       }
> }
>
> fn main(){}
> ************************************************************
>
> It is easy to write a function which does the exact same thing (for the
> struct above) and has a return type with a bounded lifetime, and therefore
> it does not make sense to me why operator overloading would be restricted
> in this fashion (i.e. restricted to return concrete lifetimes). Thanks.
>
>
>
> On Wed, Apr 16, 2014 at 2:48 AM, Eric Reed <ecreed at cs.washington.edu>wrote:
>
>> Could you provide a code sample that causes this error?
>>
>>
>> On Tue, Apr 15, 2014 at 6:28 AM, Artella Coding <
>> artella.coding at googlemail.com> wrote:
>>
>>>
>>> Currently if I try to specify lifetimes in the return types of
>>> overloaded operators like Index ([]), I get an error message :
>>>
>>> "method `index` has an incompatible type for trait: expected concrete
>>> lifetime, but found bound lifetime parameter &"
>>>
>>> Why has this restriction been placed, given that I can write custom
>>> functions which can have bounded lifetimes specifications in the return
>>> type?
>>>
>>> Thanks
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> Rust-dev at mozilla.org
>>> https://mail.mozilla.org/listinfo/rust-dev
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140421/99c7c954/attachment.html>


More information about the Rust-dev mailing list