2015-08-19 23:17:20 +08:00
|
|
|
|
- Feature Name: main_reexport
|
|
|
|
|
- Start Date: 2015-08-19
|
2017-11-01 21:29:36 +08:00
|
|
|
|
- RFC PR: [rust-lang/rfcs#1260](https://github.com/rust-lang/rfcs/pull/1260)
|
|
|
|
|
- Rust Issue: [rust-lang/rust#28937](https://github.com/rust-lang/rust/issues/28937)
|
2015-08-19 23:17:20 +08:00
|
|
|
|
|
|
|
|
|
# Summary
|
|
|
|
|
|
|
|
|
|
Allow a re-export of a function as entry point `main`.
|
|
|
|
|
|
|
|
|
|
# Motivation
|
|
|
|
|
|
|
|
|
|
Functions and re-exports of functions usually behave the same way, but they do
|
|
|
|
|
not for the program entry point `main`. This RFC aims to fix this inconsistency.
|
|
|
|
|
|
|
|
|
|
The above mentioned inconsistency means that e.g. you currently cannot use a
|
|
|
|
|
library's exported function as your main function.
|
|
|
|
|
|
2015-08-21 18:08:43 +08:00
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
pub mod foo {
|
|
|
|
|
pub fn bar() {
|
|
|
|
|
println!("Hello world!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-03 04:44:13 +08:00
|
|
|
|
use foo::bar as main;
|
2015-08-21 18:08:43 +08:00
|
|
|
|
|
|
|
|
|
Example 2:
|
|
|
|
|
|
|
|
|
|
extern crate main_functions;
|
|
|
|
|
pub use main_functions::rmdir as main;
|
|
|
|
|
|
|
|
|
|
See also https://github.com/rust-lang/rust/issues/27640 for the corresponding
|
|
|
|
|
issue discussion.
|
|
|
|
|
|
2015-10-03 04:44:13 +08:00
|
|
|
|
The `#[main]` attribute can also be used to change the entry point of the
|
|
|
|
|
generated binary. This is largely irrelevant for this RFC as this RFC tries to
|
|
|
|
|
fix an inconsistency with re-exports and directly defined functions.
|
|
|
|
|
Nevertheless, it can be pointed out that the `#[main]` attribute does not cover
|
|
|
|
|
all the above-mentioned use cases.
|
|
|
|
|
|
2015-08-19 23:17:20 +08:00
|
|
|
|
# Detailed design
|
|
|
|
|
|
|
|
|
|
Use the symbol `main` at the top-level of a crate that is compiled as a program
|
|
|
|
|
(`--crate-type=bin`) – instead of explicitly only accepting directly-defined
|
2015-10-03 04:44:13 +08:00
|
|
|
|
functions, also allow (possibly non-`pub`) re-exports.
|
2015-08-19 23:17:20 +08:00
|
|
|
|
|
|
|
|
|
# Drawbacks
|
|
|
|
|
|
|
|
|
|
None.
|
|
|
|
|
|
|
|
|
|
# Alternatives
|
|
|
|
|
|
|
|
|
|
None.
|
|
|
|
|
|
|
|
|
|
# Unresolved questions
|
|
|
|
|
|
|
|
|
|
None.
|