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 standard input line by line
Section titled “Read standard input line by line”read_line returns None at the end of the input.
import io
let mut count = 0while true { match io.read_line() { Some(line) => { if line.trim().len() > 0 { count += 1 } } None => break }}print(f"{count} non-empty lines")Read all of it at once
Section titled “Read all of it at once”import io
let text = io.read_all()print(text.split("\n").len())Write without a line break
Section titled “Write without a line break”import io
io.write("loading")io.write("...")io.write(" done\n")io.flush()loading... doneReference
Section titled “Reference”import ioFunctions
Section titled “Functions”read_line
Section titled “read_line”pub fn read_line() -> Option<Str>Returns the next line of standard input without its line ending, or None at the end.
read_all
Section titled “read_all”pub fn read_all() -> Strpub fn read(count: i64) -> StrReads 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}")