OS
Low-level OS bindings in std.os.
std.os exposes the small raw operating-system surface used by the standard
library: file descriptors, process exit, pipes, blocking mode, and basic
read/write calls.
Most application code should prefer typed modules such as std.io, std.net,
and std.future. Use std.io.print/println for simple CLI output and
std.os when a low-level API needs raw descriptor behavior.
import std.os;
fn main() {
_ = os.write("Hello world\n");
assert os.stdout != os.stderr;
}
os.write accepts raw pointer/count arguments, str, and bytes.Bytes.
Because bytes.Bytes parameters receive borrowed call-argument views, callers
can pass string literals, owned strings, existing byte views, or [u8] slices
through the same write path.
read, write, close, pipe, and set_nonblocking mirror low-level OS
operations. They report OS-style integer results and expose errno for the
last platform error.
std.os.read and std.os.write are syscall-shaped. A successful call may
transfer fewer bytes than requested, and callers that need an exact count must
loop. The str and bytes.Bytes overloads of write perform one OS write
attempt; they do not provide the complete-write guarantee of std.io.Writer.
Negative read/write results and nonzero close, pipe, or set_nonblocking
results are platform errors. Inspect os.errno immediately, before any later
OS call can replace it.
import std.mem;
import std.os;
fn main() {
let text = "hi";
unsafe {
_ = os.write(os.stdout, mem.address_of(text) as const *u8, 1);
}
let fds = [0, 0];
unsafe {
if os.pipe(fds.ptr) == 0 {
_ = os.set_nonblocking(fds[0] as usize);
_ = os.close(fds[0] as usize);
_ = os.close(fds[1] as usize);
}
}
}
| Item | Purpose |
|---|---|
stdin, stdout, stderr | Standard file descriptor constants. |
errno | Last OS error value. |
exit(code) | Terminate the process. |
read(fd, buf, count) | Read from a descriptor into raw storage. |
write(fd, buf, count) | Attempt one raw descriptor write. |
write(fd, buf) | Attempt one write of a str or bytes.Bytes value. |
write(buf) | Attempt one write to stdout. |
close(fd) | Close a descriptor. |
pipe(fds) | Create a pipe into two integer descriptor slots. |
set_nonblocking(fd) | Enable nonblocking mode for a descriptor. |
sync() | Flush filesystem buffers through the OS. |