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

前端 未结 5 1609
难免孤独
难免孤独 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条回答
  •  旧时难觅i
    2021-02-02 08:11

    std::process::exit(code: i32) is the way to exit with a code.


    Rust does it this way so that there is a consistent explicit interface for returning a value from a program, wherever it is set from. If main starts a series of tasks then any of these can set the return value, even if main has exited.

    Rust does have a way to write a main function that returns a value, however it is normally abstracted within stdlib. See the documentation on writing an executable without stdlib for details.

提交回复
热议问题