The Main function
An executable package starts at a function named Main, declared at the top level of the package — outside every module block — and taking no parameters. When Main returns, the program ends, and its result becomes the process's exit status: 0 for success, anything else for failure.
func Main() -> int {
PrintLine("Hello, Rux!");
return 0;
}
A new executable package keeps Main in Src/Main.rux, but any file of the package may hold it. Library packages have no Main, and rux run refuses to run one.
The forms
| Declaration | Ends by | Exit status |
|---|---|---|
func Main() | reaching its end, or return; | 0 |
func Main() -> int | return n; | n |
func Main() throws E | reaching its end, or return; | 0 |
func Main() -> int throws E | return n; | n |
either form with throws E | an error that reaches Main | 1 |
E may be any error type. A Main that throws lets every step pass its error on with try, out of the program:
variant StartError {
MissingConfig
}
func Load(present: bool) -> int throws StartError {
if !present {
throw StartError::MissingConfig;
}
return 3;
}
func Main() throws StartError {
let count = try Load(false); // fails: the program ends with status 1
PrintLine("loaded {}", count);
}
When an error reaches it, Main runs its ordinary cleanup — deferred statements and destructors — and the process exits with status 1. The error itself is not printed. A program that wants the user to see why it stopped prints a message first, for instance in a catch arm that then throws the same error on, e => { PrintLine("cannot start"); throw e; }.
A panic is different: it stops the program at once, without cleanup, whatever form Main has.
Any other signature — parameters, type parameters, or a result that is not an integer — is an error, whose note explains that the result of Main is the process's exit status:
func Main(count: int) -> bool {
return true;
}
The message names only what is wrong: func Main(count: int) -> bool ends with but it takes parameters and returns 'bool8', and func Main() -> float64 with but it returns 'float64'.
DllMain
A shared library built for Windows (Type = "SharedLibrary") may declare a top-level function named DllMain. The Windows loader calls it when a process loads or unloads the library, and the library loads only if it returns a non-zero value. Its signature is the one Windows expects:
func DllMain(instance: *opaque, reason: uint32, reserved: *opaque) -> int32 {
return 1;
}
Without a DllMain, the library loads as if one had returned 1. On other systems the name has no special meaning.
See also
- Function declarations — functions in general
- Errors —
throws,throwandtry - Package types — executables and libraries
- Learn: Fallible main
Function types
Functions as values: the func(T, U) -> R type, passing functions to parameters, storing them in locals and fields, function literals, and what a function value cannot be.
Overview
Declaring a struct, its fields and their visibility, building values with struct literals, reading and writing fields, value semantics and equality.