Console
Every lesson so far has printed with PrintLine. This one looks at it closely, together with its partner Print — and at the {} placeholders that put values inside text.
Print and PrintLine
The difference is one thing: PrintLine ends the line it writes, and Print leaves the cursor where it stopped, so the next output carries on the same line.
Print("one ");
Print("two ");
PrintLine("three");
one two three
A \n inside the text is a line break too, so PrintLine is Print with one added:
Print("four\nfive\n");
With nothing to print, PrintLine writes only the line ending — a blank line:
PrintLine();
Both live in Io, and the lesson imports them together with braces:
import Io::{ Print, PrintLine };
Printing values
A value can be printed on its own, with no text around it — PrintLine(42);, PrintLine(2.5);, PrintLine(true);. More often the value goes inside a message. Each {} is filled by the next argument, in the order they are written:
let name = "Rux";
let major = 0;
let minor = 4;
PrintLine("{} version {}.{}", name, major, minor);
flowchart LR
fmt["{} version {}.{}"] --> out["Rux version 0.4"]
a1["name → Rux"] -- "1st {}" --> fmt
a2["major → 0"] -- "2nd {}" --> fmt
a3["minor → 4"] -- "3rd {}" --> fmtPrint takes placeholders as well, so a line can be built from pieces:
Print("{} + {} = ", 2, 3);
PrintLine("{}", 2 + 3);
Printing a brace
To print a brace itself in text that has placeholders, double it. Text passed with no arguments is printed exactly as written, so there a single brace is fine:
PrintLine("{{}} marks a place for {}", "a value");
PrintLine("{} with nothing to fill it");
{} marks a place for a value
{} with nothing to fill it
Width, alignment and decimal places inside {} — such as {:>6} or {:.2} — come in the Format lesson.
The program
The whole lesson is one package in the Examples repository. Its comments explain every step.
// Every lesson so far has printed with `PrintLine`. This one looks at it closely, together with its
// partner `Print`. The difference between them is one thing: `PrintLine` ends the line it writes,
// and `Print` leaves the cursor where it stopped, so the next output carries on the same line.
import Io::{ Print, PrintLine };
func Main() -> int {
// Three calls, one line, because only the last call ends it.
Print("one ");
Print("two ");
PrintLine("three");
// A `\n` inside the text is a line break too, so `PrintLine` is `Print` with one added.
Print("four\nfive\n");
// With nothing to print, `PrintLine` writes only the line ending: a blank line.
PrintLine();
// A value can be printed on its own, with no text around it.
PrintLine(42);
PrintLine(2.5);
PrintLine(true);
// More often the value goes inside a message. Each `{}` is filled by the next argument, in
// the order they are written, and any kind of value can fill one.
let name = "Rux";
let major = 0;
let minor = 4;
PrintLine("{} version {}.{}", name, major, minor);
// `Print` takes placeholders as well, so a line can be built from pieces.
Print("{} + {} = ", 2, 3);
PrintLine("{}", 2 + 3);
// To print a brace itself in text that has placeholders, double it. Text passed with no
// arguments at all is printed exactly as written, so there a brace is single.
PrintLine("{{}} marks a place for {}", "a value");
PrintLine("{} with nothing to fill it");
// Watch the count. Once a call has arguments, the compiler matches them against the
// placeholders, and a placeholder with no argument, or an argument with no placeholder, is
// refused:
//
// PrintLine("{} and {}", 1);
// error: format string has 2 placeholders, but 1 argument was provided
// help: pass one argument for each '{}' placeholder
//
// Width, alignment and decimal places inside `{}` wait for the Format lesson.
return 0;
}
Run it
cd Examples/Basics/Console
rux run
one two three
four
five
42
2.5
true
Rux version 0.4
2 + 3 = 5
{} marks a place for a value
{} with nothing to fill it
Common mistakes
Once a call has arguments, the compiler counts them against the placeholders.
PrintLine("{} and {}", 1); fails with error: format string has 2 placeholders, but 1 argument was provided. Pass one argument for each {}.Try it yourself
- Print a small receipt: three items with their prices, each on its own line, and a total on the last.
- Use
Printin three calls to build1, 2, 3on one line, then end it withPrintLine(). - Print the text
{name}literally, followed by a placeholder filled with your name.
Learn more
Printin the API Reference- Format and Format number — width, alignment, precision and bases
- Input — the other direction: reading what the user types