What is the actual reason of having `->` before return type

This thread has completely derailed from the original question, and few people even seem interested in attempting to answer it at this point.

Yes, Rust has slightly different syntax than other languages. My question is simple: What is the purpose of asking? Like it or not, that is the syntax, and there's no conceivable way it will ever change.

Is this question just for historical knowledge? If so, I think this should be closed, as it seems clear that no one definitively knows (except graydon, likely). OP's history of creating threads isn't great either, but that's a separate issue.

10 Likes

This has been documented long time ago:

-> for function return type

This is to make the language easier to parse for humans, especially in the face of higher-order functions. fn foo<T>(f: fn(i32): i32, fn(T): U): U is not particularly easy to read.

https://doc.rust-lang.org/1.6.0/complement-design-faq.html#--for-function-return-type

12 Likes

When considering current rustfmt, the difference between { 4 } and = 4; are two lines and an extra indentation. The rightward drift is why "body as expression" was mentioned in RFC 2585. The alternative is to format such code with unsafe { on the same line of the function's {. Each solution has its own weirdness.

unsafe fn foo() -> Bar { unsafe { // <-- two `{` on same line
    body
}}                                // <-- two `}` on same line

// vs.

unsafe fn foo() -> Bar = unsafe {
    body
};                                // <-- note `;` at end of block

I kind of agree that discussing function-body-as-expression is off topic.

Since the question has been answered (thanks for tracking it down, @kornel!), this thread can be locked up.

2 Likes