Hacker News new | past | comments | ask | show | jobs | submit login

To be clear, Rust does not have "implicit returns" nor does it use semicolons to indicate return values.

Expressions evaluate to a value, and semicolons take an expression, throw away its value, and evaluate to () instead. That's it.

The other behaviors you're talking about fall out of these semantics, but are also different than what you've said; functions evaluate to a value, so the final expression's value determines the value it evaluates to, but you cannot "implicitly return" from the middle of a function by dropping a ;, for example.




What I meant was the fact that you can omit the "return" keyword:

  fn foo(x: i32) -> i32 { x * 2 }


Right, but only as the last expression in a block, which is different than what "implicit returns" implies.


It's a bit of a semantic distinction isn't it? In practice what it means is that you add semicolons to the end of almost every line of imperative code in a block, and you leave the semicolon off the last line and this becomes your return value implicitly (without the use of a return keyword).


It is a semantic distinction, because "everything is an expression" and "implicit returns" have different semantics.

Thinking of them as implicit returns leads people to believe they can write code like

  fn foo(x: bool) -> i32 {
      if x {
          5
      }

      // more code
and then they're confused when this doesn't work.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: