Search code examples
dprogram-entry-pointexit

D: How to exit from main?


What it the D way to terminate/exit main function?

import std.stdio;
import core.thread;

void main()
{
    int i;
    while (i <= 5)
    {
        writeln(i++);
        core.thread.Thread.sleep( dur!("seconds")(1) );
    }
    if (i == 5) 
    {
        writeln("Exit");
        return; // I need terminate main, but it's look like break do exit only from scope
    }
    readln(); // it's still wait a user input, but I need exit from App in previous step
}

I tried to googling and found next question D exit statement there is suggestion to use C exit function. Is there any new futures in modern D, that are allow to do it's more elegant?


Solution

  • Import stdlib and call exit while passing 0.

     import std.c.stdlib;
        exit(0);