"return funcname" Versus "funcname" at end of function

I don't think you can justify the use of "return funcname" at the end of a function by saying it's just a style issue, I think that "funcname" at the end of the function is the only correct style and just because the former compiles doesn't make it the former doesn't make it acceptable in properly written Rust.
What does anyone think, please?
Thank you
Tony

They're functionally equivalent, so it's certainly a style issue in my mind. I prefer the version without the return, but it's definitely not "incorrect".

I think it's far too unimportant an issue to be so terribly dogmatic.

If someone else is using explicit return, it doesn't affect you. If you don't want it in your projects, just ban it in a style guide, or require the use of rustfmt. If someone else wants it used in their project, and you're contributing to it, just deal with it--it won't kill you.

By all means, recommend it and promote it if you want, but there's no benefit to haranguing people.

 

Unless they use three spaces for indentation. Those people need to be relentlessly mocked and publicly harassed until they see sense and use five spaces like all proper, civilised people do. Damn barbarians...

9 Likes

@DanielKeep Do you mind if I submit this to TWiR Quote of the Week? :smile:

I've recently written down my thoughts on return foo vs. foo over here:

6 Likes

Thank you very much for your rapid reply Daniel. I don't think I was thinking of haranguing people or having a panic attack, I guess I wasn't sure about the issue!

Okay, thanks

Are you referring to this?

fn foo() {
  return bar;

  fn bar() { ... }
}

You need to use return in this case, which is mildly annoying, but otherwise I think it's pretty much always better to implicitly return.

I apologise if how I wrote my response caused it to come across as unduly strong. I'm also sorry that I took your post with unintended seriousness. Sometimes, communication by text is hard.[1] :slight_smile:

To be more specific, and in the hope it helps you understand how I understood your post:

The way I read your question, based on the bolded parts, was as a complete and unreserved rejection of any possible reason for using return x syntax at the end of a function. Not just in the sense of "you should avoid doing this", but in the sense of "such code shouldn't be permitted anywhere for any reason".

This sense was particularly strong given the use of "[...] by saying it's just a style issue" when it very much is a style issue.

So not just "I don't think people should do this", but as "we shouldn't even engage in discussing this and anyone who disagrees with me is just wrong".

It would be a bit like saying: "you can't justify the use of tomato sauce on meat pies by saying it's just a taste issue, I think that barbecue sauce is the only correct condiment and just because the former is available doesn't make it acceptable in polite society."

It doesn't matter that it's true, it still comes comes across as needlessly confrontational. I mean, people who like tomato sauce are suffering enough (on account of liking tomato sauce), we don't need to make things worse for them.

Also, I have seen people get extremely vitriolic about style issues on more occasions than I can count, so my interpretation of your post did not feel unreasonable. Again, I'm sorry that I misjudged your intent.

For my part, my response was not intended to be forceful. The joke at the end was to soften any perceived seriousness by deliberately undercutting it with something absurd. Like arguing about condiments.[2]


Oh dear, standards have slipped that low, have they? I suppose if nothing wittier presents itself (though that doesn't seem likely).


  1. And communication by speech. And interpretive dance. Especially interpretive dance. ↩︎

  2. Because barbecue is objectively better than tomato sauce, and everyone who thinks otherwise is just crazy. I mean, some countries even call it "ketchup" or "catsup" which is distressingly close to "cat sick". Red viscous cat sick. Bleurgh. ↩︎

2 Likes

As clippy prefers the latter style in its default settings, I consider it the correct style. People can use whatever settings in their own projects of course, but since they're functionally identical and differ little even aesthetically, I don't feel there's any good reason to bother going against the grain and using the former style.

So I see return x; at the end of a function like seeing

let x;
if b {
    x = 3;
} else {
    x = 7;
}

instead of

let x = if b {
    3
} else {
    7
};

I certainly agree about not "haranguing" people about it, but I'd also say that the latter is unambiguously better where possible.


The other good thing about usually not using it is that it makes it stand out in the places where it is useful. For example, sometimes it can be good to do

let x;
let y = if b { ... } else { x = ...; &x };

for lifetime reasons of the reference, and the less unnecessary let x;s there are, the more it helps emphasize things like this where it's worth paying extra attention.

4 Likes