std.io and std.os
API summary (generated from the Beans source by npm run coverage): 11 package functions.
These two native modules are your basic link to the terminal and the operating
system. std.io handles input and output. std.os handles arguments, the
environment, and exit. Both are built into the compiler and runtime, so there is
no stdlib/std/ source to read, and their functions are typed in the checker with
positional parameters that carry no names.
std.io
Section titled “std.io”import std.ioPrinting
Section titled “Printing”print(any)println(any)eprint(any)eprintln(any)print writes to stdout with no newline; println adds a trailing newline.
eprint and eprintln are the same, but write to stderr. Each takes one value of
any type.
What can print:
- numbers, bools, and strings, as you would expect;
- enums, shown as
variantorvariant(payload); - lists, shown as
[a, b, c].
Maps, class instances, and Result values do not print. Format those yourself
first (see std.fmt and string interpolation).
import std.io
fn main() { io.println("count is {3}") // count is 3 io.println([1, 2, 3]) // [1, 2, 3] io.eprintln("something went wrong")}Reading input
Section titled “Reading input”read_line() -> Option<string>read_all() -> stringread_line reads one line from stdin and returns none at end of input.
read_all reads all of stdin as one string.
import std.io
fn main() { let line: Option<string> = io.read_line() match line { some(text) => io.println("you typed {text}"), none => {}, }}std.os
Section titled “std.os”import std.osargs() -> List<string>env(string) -> Option<string>exit(int)args()gives you the arguments to your program. When you usebeansc run f.b -- a b, the arguments are the ones after--(soaandb). A compiled native binary reads them straight fromargv.env(name)returns the value of environment variablename, ornonewhen it is not set.exit(code)stops the program with exit codecodeand does not return.
import std.ioimport std.os
fn main() { for arg in os.args() { io.println(arg) } let home: Option<string> = os.env("HOME") match home { some(path) => io.println("home is {path}"), none => {}, } os.exit(0)}C errno, for hosted interop
Section titled “C errno, for hosted interop”When you call C code on a hosted target, two helpers in std.c let you read and
set the C errno value:
errno() -> i32set_errno(i32)errno()reads the currenterrno.set_errno(value)sets it.
Use these only when you are doing C interop and need to inspect the error a C call left behind. See the FFI guide.