Rant after trying Rust a bit

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 22 15:35:51 PDT 2015


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";


More information about the Digitalmars-d mailing list