io
Everything in io is a Stream: a handle to a file, a standard stream or a
socket. The functions here are the low-level, non-blocking layer. read and
write move as many bytes as are ready and never spin; buffers are
Vec<byte>. Errors are IoError values in a Result.
Most programs use the blocking helpers on top of this layer from coil-stdlib:
io::sync (println, write_all,
read_to_end, read_line) and io::file
(read_text, write_text):
use io::sync::{println};
use io::file::{read_text};
fn main() {
let text = read_text("notes.txt")?;
println(text)?;
}
To do several IO operations at once, run them as tasks: inside a
task::scope a task that waits for IO lets
the others run. Text conversion is in string
(to_bytes, from_bytes). File and network access needs a grant when the
program runs (--allow-read, --allow-write, --allow-net); see
Permissions. TLS is the
coil-tls package.
Built into the compiler.
Import what you use (use path::* is not allowed):
use io::{Stream, IoError};
Modules#
| Module | Description |
|---|---|
io::fs | Paths and the file system: metadata, directories, rename, copy, symlinks. |
io::file | Read and write whole files in one call. |
io::sync | Blocking IO helpers: println, read_line, write_all, read_to_end. |
io::net | TCP and UDP sockets. |
Types#
| Name | Description |
|---|---|
Stream | An open file, socket or standard stream. |
IoError | The error type of every io, io::fs and io::net operation. |
Traits#
| Name | Description |
|---|---|
Read | Types that bytes can be read from; implemented for Stream. |
Write | Types that bytes can be written to; implemented for Stream. |
Functions#
| Name | Description |
|---|---|
stdin | Returns a new stream on the process’s standard input. |
stdout | Returns a new stream on the process’s standard output. |
stderr | Returns a new stream on the process’s standard error. |
capture_begin | Starts keeping this thread’s stdout and stderr writes in memory instead of writing them, until the matching capture_end(). |
capture_end | Ends the innermost capture_begin() and returns what it kept, as (stdout, stderr). |
open | Opens the file at path with mode and returns a stream on it. |
close | Closes s. |
read | Reads bytes from s into the front of buf without waiting. |
write | Writes bytes from buf to s without waiting and returns how many were written. |
write_from | Writes buf[offset..] to s without waiting and returns how many bytes were written. |
wait_readable | Waits until s is readable. |
wait_writable | Waits until s is writable. |
await_readable | Older name of wait_readable; behaves exactly the same. |
await_writable | Older name of wait_writable; behaves exactly the same. |
drive | Deprecated: polls the registered IO waiters once and returns how many became ready. |
wait_ready | Deprecated: waits until any registered IO waiter is ready and returns how many became ready. |
result_unit_probe | Returns Ok(()) when n >= 0 and Err(IoError::InvalidInput) otherwise. |
attach | Installs package IO hooks on s in place; the free-function form of Stream.attach. |
park | Waits until the descriptor of s is ready; the free-function form of Stream.park. |