Why does Rust not have a return value in the main function, and how to return a value anyway?

前端 未结 5 1591
难免孤独
难免孤独 2021-02-02 07:38

In Rust the main function is defined like this:

fn main() {

}

This function does not allow for a return value though. Why would a language not

5条回答
  •  迷失自我
    2021-02-02 08:02

    The reddit thread on this has a "why" explanation:

    Rust certainly could be designed to do this. It used to, in fact.

    But because of the task model Rust uses, the fn main task could start a bunch of other tasks and then exit! But one of those other tasks may want to set the OS exit code after main has gone away.

    Calling set_exit_status is explicit, easy, and doesn't require you to always put a 0 at the bottom of main when you otherwise don't care.

提交回复
热议问题