News for hackers. Stuff that matters.
Re:Things Rust shipped without by pcwalton on Friday July 03, 2015 @03:26PM (#9827493)
Implicit returns encourage functional style; foo.map(|x| x + 1) is so much nicer than foo.map(|x| { return x + 1; }). Once you have implicit returns in closures, you might as well have them everywhere for consistency.
Re:Things Rust shipped without by veddan on Friday July 03, 2015 @03:57PM (#9827618)
I think that the "expression-style" return looks good for short and "expression-like" function.

Good

    fn inc(a: u32) -> u32 { a + 1 }
    fn foo(a: u32, b: u32) -> u32 { let x = a + b; a * x }
Bad

    fn bar(...) -> bool {
        let mut success = false;
        let conn = getConnection();
        ...
        if x > y {
            return false;
        } else if z < q {
            success = false;
        }
        foo.barify(x, y);
        ...
        success
    }
It looks especially bad when the function has multiple early returns, and then the final return looks different.
Re:Things Rust shipped without by ewillbefull on Friday July 03, 2015 @08:03PM (#9828562)
This could easily be added as a lint to the codebase.
Re:Things Rust shipped without by Eerie on Friday July 03, 2015 @04:26PM (#9827748)
Sheesh, it's such a little thing. "success" vs "return success;"
Re:Things Rust shipped without by cmrx64 on Friday July 03, 2015 @06:13PM (#9828209)
Doesn't work when you're not returning from a function (ie, EVERY OTHER PLACE a block can appear)
Re:Things Rust shipped without by 88e282102ae2e5b on Friday July 03, 2015 @04:18PM (#9827715)
Why "might as well"? Things can be optimal in some places and suboptimal elsewhere.
Re:Things Rust shipped without by lmm on Friday July 03, 2015 @04:46PM (#9827865)
Consistency is valuable. If some blocks have different rules to other blocks that's a real downside.
Re:Things Rust shipped without by 88e282102ae2e5b on Friday July 03, 2015 @05:26PM (#9828040)
That's true, but they're so syntactically different that I can't imagine a scenario where someone is asking whether it's closures or functions that you're supposed to use return statements in.
Re:Things Rust shipped without by Sanddancer on Friday July 03, 2015 @09:28PM (#9828776)
A foolish consistency is the hobgoblin of little minds. Sometimes a variation in rules allows for more clarity -- one could set their editor to make return a different color, for example, making it easier to glance at program flow. Then again, I'd argue that closures should allow returns too, like they do in C#. I'm an adamant supporter of the explicit camp -- when you have to debug things at 3 in the morning, sometimes that extra little bit of context can make things blindingly obvious.
Re:Things Rust shipped without by lmm on Saturday July 04, 2015 @04:39AM (#9829656)
I find the way it works in scala very nice - a block is just an expression that evaluates to the last statement in the block, and idiomatic code never uses "return" anywhere. Even e.g. a function definition, you can replace the block with a single expression if it's more convenient. I agree with being explicit but I don't think return actually makes things any more explicit (if you're talking about an editor highlighting, the editor knows which line is the value of the block, with or without a "return") - rather it's just syntactic ceremony. It's surprising how much difference having very low-overhead closures makes - you can make so much of your program simpler, because it's not a problem if a caller needs to make a slight modification to something.
Re:Things Rust shipped without by pcwalton on Friday July 03, 2015 @10:34PM (#9828896)
Closures do allow returns in Rust.
Re:Things Rust shipped without by carterehsmith on Friday July 03, 2015 @08:30PM (#9828624)
> foo.map(|x| x + 1) is so much nicer than foo.map(|x| { return x + 1; })

"Nicer" is a subjective thing. BTW in the trivial case above one may judge this or that to be nicer, but in a large method, 'return func(a,b,c)' is obvious, whereas looking at 'func(a,b,c)' it is super non-obvious that the value is being returned.

Re:Things Rust shipped without by nbouscal on Friday July 03, 2015 @11:42PM (#9829080)
It's not at all non-obvious if you learn the language. It's also not non-obvious if you learn any of the myriad other languages with the same semantics (including popular web languages like Ruby and CoffeeScript).
Re:Things Rust shipped without by jnordwick on Saturday July 04, 2015 @03:15AM (#9829517)
compromise. Make return very short, like a unary colon ":".

Then "foo.map(|x| :x+1)" is nice. If that doesn't stand out enough for some people on a line of its own, make your editor render the unary colon line in a very bold color for you.

Re:Things Rust shipped without by eddyb on Saturday July 04, 2015 @03:48AM (#9829576)
That doesn't solve the fact that blocks and conditional control flow (if/else and match) are expressions and need a way to produce values.