Rant after trying Rust a bit

Andre Kostur via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 23 10:19:05 PDT 2015


On 2015-07-22 3:35 PM, John Colvin wrote:
> On Wednesday, 22 July 2015 at 21:36:58 UTC, jmh530 wrote:
>> On Wednesday, 22 July 2015 at 20:43:04 UTC, simendsjo wrote:
>>> When "everything" is an expressions, you can write things like
>>>     auto a = if(e) c else d;
>>>
>>> In D you have to write
>>>     type a = invalid_value;
>>>     if(e) a = c;
>>>     else  a = d;
>>>     assert(a != invalid_value);
>>>
>>
>>
>> I prefer this example from one of the various Rust tutorials
>>
>> let foo = if x == 5 {
>>                 "five"
>>           }
>>           else if x == 6 {
>>                 "six"
>>           }
>>           else {
>>                 "neither"
>>           }
>>
>> You're basically using a conditional expression as an rvalue. You can
>> do the same thing with a { } block.
>
> Admittedly nowhere near as clean, but if you can bear to see the
> "return"s, function literals can turn any bunch of code in to an
> expression:
>
> auto foo = { if(x == 5)
>                           return "five";
>                       else if(x == 6)
>                           return "six";
>                       else
>                           return "neither";
>                     }();
>
> or of course there's the perhaps overly terse (brackets optional, i like
> them to visually group the condition with the ? ):
>
> auto foo = (x == 5)? "five"
>                     : (x == 6)? "six"
>                     : "neither";


Shouldn't that be its own function anyway?  If you needed it in one 
place, you'll probably need it elsewhere.  And, in this case, it can 
even be marked as pure.


More information about the Digitalmars-d mailing list