Skip to content

io

Standard input and output beyond print. Use it to read what another program pipes in, or to write output without a line break.

print writes a line to standard output. io.write writes text as it is, and io.flush sends buffered output on at once.

read_line returns None at the end of the input.

import io
let mut count = 0
while true {
match io.read_line() {
Some(line) => {
if line.trim().len() > 0 {
count += 1
}
}
None => break
}
}
print(f"{count} non-empty lines")
import io
let text = io.read_all()
print(text.split("\n").len())
import io
io.write("loading")
io.write("...")
io.write(" done\n")
io.flush()
loading... done
import io
pub fn read_line() -> Option<Str>

Returns the next line of standard input without its line ending, or None at the end.

pub fn read_all() -> Str
pub fn read(count: i64) -> Str

Reads up to count bytes of standard input; fewer only at the end.

Reads bytes, not characters: a count can end in the middle of a multi-byte UTF-8 character. Returns an empty string at the end of the input.

pub fn write(text: &Str)

Writes text to standard output without adding a line break.

pub fn flush()

Output is buffered; a program talking to another one must flush after each message.

Output is buffered. A program that answers another program over a pipe (a language server, a REPL behind a prompt) must call flush after each message, or the other side waits for text that is still in the buffer. A prompt written with write before read_line needs a flush too.

import io
io.write("name? ")
io.flush()
let name = io.read_line().unwrap_or("")
print(f"hello {name}")