Home / Learn / 1 Hello, Una
1.3 The main function
Page 4 of 11
1.3.1 What main is
A program has many functions, and one of them is where a run starts: main. Think of the first step of a recipe: you may read the recipe in any order, but you cook it from step one. When you type una run ., una finds the module that entry names in una.toml, finds its function main, and follows the steps of main from the first to the last. When the last step ends, the program ends.
1.3.2 Why a program has one starting point
A module may declare many functions, in any order, as section 1.2 shows. Without a fixed starting point, nobody could tell which of them runs first. With main, the answer is always the same: una run . starts at main of the entry module, and everything else runs because main, or a function it calls, asks for it.
1.3.3 How to write main
Here is a main with three steps:
module greeting
--- Greets the first customers of the day.
pub main() -> Unit
uses @console: Console[print]
=
@console.print("Good morning")
@console.print("The store is open")
@console.print("Today's special: desk lamps")pub. Makesmainpublic: reachable from outside its module, which is howuna run .reaches it. Chapter 6 coverspubin full.main. The name, exactlymain.(). The parameters, the inputs of the function.maintakes none, so the parentheses are empty.-> Unit. What the function gives back when it ends.Unitmeans nothing useful:mainruns for what it does, not for a value.uses @console: Console[print]. What the function may do outside the program: print. Section 1.5 explains the line.=, then the body. The body is the list of steps, each on its own line and indented by two spaces under=. The first lines, frompubto theusesline, are the function's header.
The package's una.toml names the module greeting as its entry:
[package]
name = "greeting"
version = "0.1.0"
edition = "0.2"
[run]
entry = "greeting"
grants = ["console"]una run . prints:
Good morning
The store is open
Today's special: desk lamps1.3.4 How main behaves
- The steps run in order. Each step runs to its end before the next one starts, from the top of the body to the bottom.
- The program ends with
main. When the last step ofmainends, the program ends. mainis found by its header.una run .looks in the entry module for exactlypub main() -> Unit. A module without one may still check clean, since it may be meant for other modules to use, butuna run .refuses to start it, and says that the module has nopub main() -> Unitto run. The same happens whenmainlackspub.- The entry names the module.
entry = "greeting"names the modulegreeting, the filesrc/greeting.una. A package has one entry, and so onemainthatuna run .starts.
1.3.5 When the program grows
main is where a run starts, not where all the work must be. As a program grows, its steps move into functions of their own, and main calls them in order, so that it reads like a table of contents: first the greeting, then the hours, then the offers. Section 1.4 shows how to write such functions.
1.3.6 Check yourself: main
The sign on a store's front door is printed by this program:
--- Prints the sign for the front door, with an empty line between its two parts.
pub main() -> Unit
uses @console: Console[print]
=
@console.print("Welcome")
@console.print("")
@console.print("Open from 9:00 to 18:00")
@console.print("Closed on public holidays")What does una run . print?
Show the answer
Welcome
Open from 9:00 to 18:00
Closed on public holidaysFour lines, in the order of the four steps. The second step prints an empty text, "", which is a line with nothing on it.