println writes a line to the host runtime’s standard output. It is the output
function used throughout this book.
fn main() void {
println("hello")
}It takes exactly one string, so values are formatted before the call — most
often with interpolation:
fn main() void {
name := "ada"
count := 3
println("${name} has ${count} items")
}There is no + for strings — a concatenation allocates, so it is written as
an interpolation or an append; see
Building strings.
Standard error: eprintln
eprintln writes a line to standard error, the stream for messages about the
run rather than its output — a caller that pipes a program’s output on still
sees them, and they never end up in the data:
fn main() void {
println("id,total")
eprintln("warning: 3 rows skipped")
}It takes one string and carries no effect, as println does.
Is it a terminal?
console.stdout_is_terminal(), in module console, is true when the
program’s standard output reaches a terminal, and false when it is piped,
redirected to a file, or collected by atoll test. Colour codes belong only on
a terminal — anywhere else they are carried on as bytes:
import console
fn warn(msg string) string {
if console.stdout_is_terminal() {
return "\u{1b}[33m${msg}\u{1b}[0m"
}
return msg
}
fn main() void {
println(warn("3 rows skipped"))
}Interpolation uses Display
"${value}" calls the value’s write_to through the
Display trait, which appends its text
to the string the template is building, so implementing Display makes a user
type printable:
struct Point { x int, y int }
impl Display for Point {
fn write_to(&self, out &mut string) void {
out.append("(")
self.x.write_to(out)
out.append(", ")
self.y.write_to(out)
out.append(")")
}
}
fn main() void {
println("${Point { x: 1, y: 2 }}")
}A nested " inside an interpolation hole is not supported — bind the string to
a local first rather than escaping it.
println does not suspend
println is declared @host with no @suspend, the core’s std.println: it puts
the line in order and returns, and a function that calls it carries no
effect, so it may be called from a Drop body and from a defer on the
cancellation edge, both of which refuse a body that suspends.
The write is synchronous: when println returns, its line is committed
behind everything the program wrote before it, to standard output or standard
error, and nothing reorders or loses it. It is not blocking: the line is
copied into the run’s output queue, and a host thread makes the system calls, a
batch of lines at a time, so a program printing to a slow terminal or a full
pipe goes on running. The program waits only where it has to. A run ends with
its output written, and so does a read of standard input that has to wait — a
prompt is on the terminal before the program sleeps — and starting a process
that shares the streams. A program that prints megabytes faster than its reader
reads waits for the writer rather than holding its output in memory without
bound. eprintln is written the same way, in the same queue, so the two
streams keep the order the program wrote them in.
fn report(total int) void {
println("total=${total}")
}
fn main() void {
report(7)
}That program checks clean and runs. A call that waits — a file read, a network
request, time.sleep — is @suspend, and a function that makes one inherits the
Suspend effect. You never write that down either: effects are inferred, and a
function with no using clause is told nothing about them. See
Effects.
Capability and redirection
println routes through the host boundary rather than writing directly, which
has two consequences:
- A project whose
[host] allowgrants neitherstd.printlnnorstdis refused at compile time, at the call, rather than silently doing nothing. - An embedder can divert output into a buffer instead of the process’s stdout, which is how the runtime’s tests capture program output.
There is no print without a newline and no formatted-print variant: build the
complete line as a string and pass it. For levelled events with fields, which
a run turns on and off, see Tracing and metrics.
Standard input, and a claimed standard output: std/console
A program that another program drives through its standard streams (an MCP
server a coding agent launches, an agent an editor runs, a filter in a pipe)
imports console. console.read_line() reads the next line of standard input,
without its \n or \r\n, and answers None once the input has ended. It
waits for a whole line; a read whose task is cancelled while it waits answers
at once and takes nothing, so the next read is handed the line.
console.claim_stdout() claims standard output for the Stdout it answers.
From then on write_line is its one writer, a whole line a call, and a
println anywhere else, the program’s own or a dependency’s, goes to standard
error, where it cannot break a message of the protocol. A second claim is
refused with AlreadyClaimed.
import console
fn main() void {
out := match console.claim_stdout() {
Ok(o) => o
Err(e) => return
}
mut n := 0
for {
match console.read_line() {
Ok(read) => {
if Some(line) := read {
n = n + 1
out.write_line("${n}: ${line}")
} else {
return
}
}
Err(e) => eprintln("unread: ${e}")
}
}
}The input is read on a thread of the host’s, started by the first read: a
program that never reads starts none, and one whose read is parked on a
terminal that never sends still ends. In atoll test, what a claim writes is
the test’s printed output, and the claim ends with the test.