Printing and input/output
The console calls print, println, eprint and eprintln, the two output streams, flush, and reading a line with std::io.
Almost every program in this guide used println. This page lists the
console calls, explains the two output streams, and shows how to read a line
that a person types.
The console calls
There are four console calls. Each one writes one value, and a label before the colon says what kind of value it is:
| Call | Writes to | Ends the line |
|---|---|---|
println | standard output | yes |
print | standard output | no |
eprintln | standard error | yes |
eprint | standard error | no |
A console call is an action with one participant: the value it writes. The call only looks at that value, so the value is in the same state after the call as before it. These are the labels:
| Form | Writes |
|---|---|
println(string: text) | a String |
println(int: number) | an integer of any type up to 64 bits |
println(byte: number) | a u8 as a decimal number |
println(bool: flag) | true or false |
println(format: template, int: n) | the template with {} replaced by an integer |
println(format: template, bool: b) | the template with {} replaced by true or false |
println(buffer: abs bytes) | the bytes of a Buffer, exactly as they are |
print, eprint and eprintln take the same labels. A call must have a
label: println(x) without one is refused.
println(int: ...) takes any integer type up to 64 bits: i8 to i64, u8
to u64, Usize, Int, and odd widths such as u33. The exact value is
written, so you do not need as Int. An integer type wider than 64 bits, such
as i128, is refused with the error E1139. Convert it to a 64-bit type with
as first, as shown in Numbers. println(byte: ...) is
the form for a u8. The byte is written as a number, not as a character.
In a format: template, {} marks the place of the value. The template has
one {}, and the value after it is either an int: or a bool:. The int:
value follows the same rule as println(int: ...).
A console call has no value. It is a statement on its own, and a program does
not learn whether the write worked. If you use a call as a value, for example
erg done = println(string: "x");, the compiler refuses it with E1141.
print and println
println writes the value and then a newline. print writes exactly the
value and nothing else, so the next call continues on the same line:
Bytes
A Buffer is a block of bytes. Buffer[0] makes an empty one, and
append(bytes, 72u8) adds one byte. The number 72 is the letter H and 105 is
i. print(buffer: abs bytes) writes the bytes as they are, with no newline,
so the next call continues on the same line. The buffer is only looked at, so
you write it with abs:
The first Hi is followed by | on the same line. That | comes from a
println(string: ...), which then ends the line.
Two output streams
A program has two places to write to. println and print write to the
standard output, which is where normal results go. eprintln and eprint
write to the standard error, which is for messages about problems. In a
terminal you see both. When you redirect the output to a file or to another
program, the two streams can be separated.
The first command throws away the standard error, so only the println lines
show. Now the other way round:
Here only the standard error remains. The last line, process exited with status 0, is not printed by your program. It is printed by actus run on
the standard error, after your program ends.
flush
Output that does not end in a newline can wait in a buffer. It is written
when the program ends. flush() writes it out at once. It has no label, and
nothing to look at, so it takes no argument. This matters when you want a
prompt to appear before the program waits for input:
std::io
The console calls work in every program and need no import. For reading
input, there is the library module std::io. Add this to the top of a file
to use it:
std::io is part of the standard library. It is available when the
Actus.toml of your project says runtime = "std", which is the setting a
new project from actus new has. Without it the compiler reports that the
module is not part of the build. A plain import std::io; gives you the name
io, so you write io::read_line(...).
Reading a line
io::read_line reads one line from the standard input into a Buffer. The
buffer belongs to you, so you lend it with ins, as in
Ownership roles. read_line returns a Result:
Ok when it read something, Err with an IoError when it did not. When
there is no more input, the error is IoError.EndOfStream.
The command line shows how to give the program some input: echo writes a
line and the | sends it to the standard input of actus run. The program
wrote a prompt with print, flushed it, read the line, and wrote it back
with println(buffer: ...).
Now run it with no input, as if the user closed the input at once:
This time read_line returned EndOfStream. The program wrote a message to
the standard error and returned 2, which is the exit status you see in the last
line. Any other kind of IoError is handled by the _ arm, which returns 1.
The program decides what each outcome means. The Result type and case are
explained in Errors: Result and Option.
What you learned
printlnwrites a value and a newline.printwrites the value only. The labels arestring:,int:,byte:,bool:,format:andbuffer:.eprintlnandeprintdo the same on the standard error.- A console call needs a label, has no value, and does not change the value it writes.
flush()writes out what is waiting on the standard output.import std::io;gives youio::read_linefor projects that useruntime = "std".io::read_line(buffer: ins line)reads a line and returns aResult.
Where to go next
You have now seen the basics of the language. Continue with Tools to learn how to split a program into modules, how to test it and which commands to run while you work. You can also go back to the Introduction for the bigger picture, or read the other pages again with your own programs in mind.