Understanding `return` and how it fits conceptually with other expressions

⚓ Rust    📅 2025-06-29    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 41 days ago. The information described in this article may have changed.

Had some explanations elsewhere about the "weird" function:

fn strange() -> bool {let _x:bool = return true;}

I accept that:

  1. return true; exits the function with that value (true)
  2. Has an associated type as an expression (although it has no value) which is !
  3. In that specific case, the ! can coerce to bool (or anything else) so it passes the type checking.

In other words, the "argument" to return (output of the function) and the type of it's evaluation (although no value appears) are separate things.

One initial problem is that it does not match the definition of expression (it yields no value). The other issue is that to an extent a value does result from it, even though it's not assigned to a but to the function's output.

I've seen other examples using !unreachable() or continue or break in the std documentation for the never type-!

It's also clear that it's called "never" because it's got no value.

To me, without much knowledge so far, but intuitively from reading code, it feels that one is accepting a little bit of an irrational statement.

Besides some details that can be wrong here an there in my explanation above, what are some important conceptual frames for this? Or how do you see it (does not need to match a strict definition)?

PS: I did read -and will keep doing- related threads, but I'd like to have some new conversation about it.

1 post - 1 participant

Read full topic

🏷️ rust_feed