You are on page 1of 1

Chapter 1 Printing on the Terminal

For example:

fn Main() {}

If you compile this program, you get the compilation error "main function not
found", as no main function (with a lowercase m) is defined in the program.
From now on, except when specified, we will assume the example code will be inside
the braces of the “main” function, and so the braces and the text preceding them will be
omitted.

Printing Combinations of Literal Strings


Instead of using a single literal string, you can print several of them, even in a single
statement. In this way:

print!("{}, {}!", "Hello", "world");

This statement, put inside the braces of the “main” function, will print again:
"Hello, world!".
In this case, the print macro receives three arguments, separated by commas.
All three arguments are literal strings. The first string, though, contains two pairs of
braces ({}). They are placeholders, indicating the positions in which to insert the other
two strings.
So, the macro scans the arguments after the first one, and for each of them it
looks inside the first argument for a pair of braces, and replaces them with the current
argument.
This resembles the following C language statement:

printf("%s, %s!", "Hello", "world");

But there is an important difference. If you try to compile

print!("{}, !", "Hello", "world");

you get the compilation error "argument never used", as the arguments of the macro
after the first one are more numerous than the placeholders inside the first argument;
that is, there is some argument that doesn't have any corresponding placeholder.
And if you try to compile

print!("{}, {}!", "Hello");

You might also like