[][src]Attribute Macro termination::display

#[display]

Procedural macro that allows main return values to use Display instead of Debug.

Any form of fn main() -> _ should produce practically identical results with, or without this annotation, the only difference being the trait used to format the error values for printing.

Additionally, Termination, (not to be confused with std::process::Termination) is used by the annotated main instead.

Examples

Functions that return unit type work the same.

#[termination::display]
fn main() -> () {
    ()
}

It is possible to directly return exit code.

#[termination::display]
fn main() -> i32 {
    0
}

Default implementation of Termination for Result<(), E> where E: Display prints error to stderr, and returns appropriate error code to the operating system.

#[termination::display]
fn main() -> Result<(), &'static str> {
    Err("Error: failed successfully.")
}

It is still possible to use ! as a return type; modified main never returns, therefore macro-generated code will most likely be stripped out by compiler.

#[termination::display]
fn main() -> ! {
    std::process::exit(0);
}

Custom implementations also are possible.

use termination::{display, Termination};

struct HelloWorld;

impl Termination for HelloWorld {
    fn report(self) -> i32 {
        println!("Hello world!");
        42
    }
}

#[display]
fn main() -> HelloWorld {
    HelloWorld
}