Printing and input/output

View source

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:

CallWrites toEnds the line
printlnstandard outputyes
printstandard outputno
eprintlnstandard erroryes
eprintstandard errorno

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:

FormWrites
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.

actus
verb main() {
    erg level: u8 = 200u8;
    erg ready = true;
    println(string: "text");
    println(int: 42);
    println(byte: level);
    println(bool: ready);
    println(format: "{} apples", int: 3);
    println(format: "level: {}", int: level);
    println(format: "ready: {}", bool: ready);
}
bash
actus run
text
text
42
200
true
3 apples
level: 200
ready: true
process exited with status 0

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.

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:

actus
verb main() {
    print(string: "one ");
    print(int: 2);
    print(string: " ");
    println(bool: true);
    println(string: "next line");
}
bash
actus run
text
one 2 true
next line
process exited with status 0

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:

actus
verb main() {
    erg bytes = Buffer[0];
    append(bytes, 72u8);
    append(bytes, 105u8);
    print(buffer: abs bytes);
    println(string: "|");
    println(buffer: abs bytes);
}
bash
actus run
text
Hi|
Hi
process exited with status 0

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.

actus
verb main() {
    println(string: "to stdout");
    eprintln(string: "to stderr");
    eprintln(int: 7);
    eprintln(bool: false);
    println(int: 8);
}
bash
actus run 2>/dev/null
text
to stdout
8

The first command throws away the standard error, so only the println lines show. Now the other way round:

bash
actus run >/dev/null
text
to stderr
7
false
process exited with status 0

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:

actus
verb main() {
    print(string: "name: ");
    flush();
}

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:

actus
import std::io;

verb main() {
    println(string: "io is imported");
}

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.

actus
import std::io;

verb main() -> Int {
    print(string: "name: ");
    flush();
    erg line = Buffer[0];
    erg result = io::read_line(buffer: ins line);
    return case dat result {
        Result.Ok(_) => {
            print(string: "hello, ");
            println(buffer: abs line);
            return 0;
        },
        Result.Err(error) => case dat error {
            IoError.EndOfStream => {
                eprintln(string: "no input");
                return 2;
            },
            _ => {
                eprintln(string: "read failed");
                return 1;
            },
        },
    };
}
bash
echo "Ada" | actus run
text
name: hello, Ada
process exited with status 0

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:

bash
actus run < /dev/null
text
name: no input
process exited with status 2

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

  • println writes a value and a newline. print writes the value only. The labels are string:, int:, byte:, bool:, format: and buffer:.
  • eprintln and eprint do 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 you io::read_line for projects that use runtime = "std".
  • io::read_line(buffer: ins line) reads a line and returns a Result.

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.