5

Looking at question number 3 here.

As an example, I've edited as such.

fn main() {
    never_return();
    // println!("Failed!");
}

fn never_return() -> ! {
    // Implement this function, don't modify the fn signatures
    panic!("stop")
}

The expectation when returning something from a fn is without a trailing ;. In the above case, the panic!(_) returns a type never and does what I would expect it to. However, the same fn signature, returning !, compiles to the same regardless of whether there is a ; after the panic macro or not. I'm assuming this is the case because of the intrinsics of a panic? But couldn't find a technical explanation to it that I understood.

Why is this the case?

kenta_desu
  • 303
  • 1
  • 9

1 Answers1

3

I think you're misunderstanding the ! type. The panic! macro does not "return a type never", rather it never returns. By using a fn foo() -> ! signature, you're declaring that this function never actually returns, and invoking another function/macro that never returns satisfies that. Similarly, the following compiles:

fn never_returns() -> ! {
  loop { }
}

since it loops forever, it never returns