@nikomatsakis This is turning out to be more complicated/controversial than initially anticipated and I am starting to not like it.
If the objective is to simplify all those examples in the internet, here is a radically different alternative:
A Rust Scripting Mode (look ma’, no main)
We could add a new rule for binary crates (if the main function is not provided, then…) that turns on the “Rust Scripting Mode”, which is stable Rust with some extra features enabled. Some features that we could enable:
-
rust_script_main: if main is not found, the whole file is interpolated into a rust_script_main function and “all rust script mode features are enabled”.
- operator
?: if using operator ? in the outer-most scope of rust_script_main fails:
-
std::os::process_exit(FAILURE) is called (for anything more complicated, there will always be main).
-
last expression: if the last expression returns an std::os::ExitCode, std::os::process_exit is called with that code, otherwise it is called with SUCCESS (failure and success should be defined for every supported platform anyways).
-
use/extern-crate reordering: inside rust_script_main you can add use/extern crate statements wherever you want, macros must be imported before first usage.
-
// /usr/bin/rustc: A comment in the first line to allow the script to be “executable” by rustc (it would just compile it, and then execute the binary).
One could probably add some other things, but the objective could be to make writing throw-away scripts as pleasant as possible. Those who want control (like more complex error handling in main, can always write their own main function).
An automatic transformation to a proper Rust program with main function could be provided by e.g. rustfmt in case some scripts become “too popular”.
EDIT: By this I do not mean that we should not pursue something like permitting ? in main(), but rather, that if our main objective (haha) is to simplify those examples, there might be other solutions that we might like to explore.