D vs Rust: function signatures

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 30 13:55:35 PDT 2014


On 04/30/2014 04:04 AM, Vladimir Panteleev wrote:
>>
>> fn map<'r, B>(self, f: |A|: 'r -> B) -> Map<'r, A, B, Self>
>
> Same as points 1 and 3 above (D's version allows specifying multiple
> functions).
>
> Not sure what 'r or |A| means in Rust syntax, but I guess this would be
> the equivalent D syntax:
>
> auto map(R)(R delegate(T))

|A| -> B is the type of a closure mapping an A to a B. 'r is a lifetime 
parameter (there is another one in Self):

I.e. that signature is roughly saying: The returned iterator lives at 
most as long as the closure context of f and the underlying iterator.

This way you can eg. get an iterator over some mutable data structure, 
map it without allocations using a stack closure, and the type system 
verifies that the data structure is not changed while the mapped 
iterator is in use, and that there are no dangling references to stack 
memory left behind.


More information about the Digitalmars-d mailing list