← Back to lessons

33 Console IO

Hard

Input and output from the console is done through std.console.

These function calls return an option type because the console may be unable to read any data if it is redirected to an empty file. We can use getOrThrow to access the contents. Using Console.stdOut.write() ensures thread safety.

IO.cj
import std.console.*

main() {
    // reads full line
    let s1 = Console.stdIn.readln()
    // takes rune as input
    let s2 = Console.stdIn.readUntil(r" ")

    Console.stdOut.writeln(s1.getOrThrow())
    Console.stdOut.writeln(s2.getOrThrow())
    Console.stdOut.flush()
}