According to The Rust Reference,
If a
mainfunction is present, (snip), and its return type must be one of the following:
()
Result<(), E> where E: Error
but it doesn't say what happens when main() returns (), Ok(()) or Err(<value>).
As far as I tested,
() |
Ok(()) |
Err(<value>) |
|
|---|---|---|---|
| Exit Status | 0 | 0 | 1 |
| Additional Behavior | - | - | Error: <value> is printed to stderr |
Are these behaviors defined, explicitly explained or guaranteed in some documentation? In particular, can I assume
a program always exits with
1status whenmain()returnsErr(<value>)?the error message displayed when
main()returnsErr(<value>)is always of the formError: <value>?
Notes:
I want some sort of documented guarantee rather than an empirical explanation. This is why I added
#language-lawyertag.This question is not about When should I use
()and when should I useResult<(), E>? or such. One can find answers (or at least hints or criteria) to such questions in many documentations or tutorials, as you know.
Updates:
Termination trait is finally stabilized in Rust 1.61.0 (source).