std::fsFile system operations.
Read, write, and query files on the local file system.
For path-only helpers prefer std::path, and for stdin/stdout prefer
std::io. This module keeps fs.exists as a compatibility surface
for existing code.
IoError is the stable user-facing error enum for file-system failures.
Use the try_* wrappers in this module—such as try_read,
try_write, try_rename, and try_copy—with Result<T, IoError>
and the ? operator for clean error propagation:
fn load_config(path: string) -> Result<string, IoError> {
fs.try_read(path)
}
fn main() {
match load_config("config.toml") {
Ok(data) => println(data),
Err(e) => {
match e {
IoError::NotFound(_) => println("missing"),
_ => println("i/o error"),
}
},
}
}
io_error_from_errnoMap an OS errno integer to a structured IoError variant.
The i64 payload carried in each variant is the raw OS errno so callers
can inspect the platform value. errno=0 (no OS error) maps to Other(0).
Covers both file-system and network errno values so this function can be
shared across fs, net, stream, http, tls, quic, dns, and
websocket modules.
io_error_messageReturn a human-readable description of an IoError.
The returned string includes the variant name and the raw OS errno so callers can log or surface structured errors without matching on every arm:
match result {
Err(e) => panic("operation failed: " + fs.io_error_message(e)),
Ok(_) => {},
}
io_error_timed_outConstruct an IoError::TimedOut(0) value without a platform-specific
errno. Useful for in-process deadline expiry where there is no OS error
to map (e.g. fs.io_error_from_errno would need a platform-aware errno
constant that the caller does not have).
io_error_cancelledConstruct an IoError::Cancelled(0) value without a platform-specific
errno. Useful for in-process cancellation (e.g. blocking pool stopped)
where there is no OS error to map.
readRead the entire contents of a file as a UTF-8 string.
Panics if the file does not exist or cannot be read.
Use try_read for a structured Result<string, IoError> surface.
let src = fs.read("main.hew");
try_readRead the entire contents of a file as a UTF-8 string.
Returns Err(IoError) instead of panicking on open failures.
writeWrite a string to a file, creating or overwriting it.
Returns 0 on success, non-zero on error.
Use try_write for a structured Result<i64, IoError> surface.
try_writeWrite a string to a file, creating or overwriting it.
Lifts the native status code into Result<i64, IoError>.
appendAppend a string to the end of a file.
Creates the file if it does not exist. Returns 0 on success.
Use try_append for a structured Result<i64, IoError> surface.
try_appendAppend a string to the end of a file.
Lifts the native status code into Result<i64, IoError>.
existsCheck whether a file exists at the given path.
if fs.exists("config.toml") {
println("found config");
}
deleteDelete a file. Returns 0 on success.
try_deleteDelete a file.
Returns Err(IoError::NotFound(_)) when the path is missing.
sizeReturns the size of a file in bytes.
read_bytesRead the raw bytes of a file into a bytes value.
Returns an empty bytes buffer on error.
Use try_read_bytes for a structured Result<bytes, IoError> surface.
let data = fs.read_bytes("image.png");
println(data.len());
try_read_bytesRead the raw bytes of a file into a bytes value.
Returns Err(IoError) instead of an ambiguous empty buffer on failure.
write_bytesWrite a bytes value to a file, creating or overwriting it.
Returns 0 on success, non-zero on error.
Use try_write_bytes for a structured Result<i64, IoError> surface.
try_write_bytesWrite a bytes value to a file, creating or overwriting it.
Lifts the native status code into Result<i64, IoError>.
mkdirCreate a directory. Returns 0 on success, -1 on error.
Fails if any parent directory does not exist. Use mkdir_all to create
parent directories automatically.
try_mkdirCreate a directory with structured IoError results.
mkdir_allCreate a directory and all its parent components.
Returns 0 on success, -1 on error.
try_mkdir_allCreate a directory and all its parent components.
Lifts the native status code into Result<i64, IoError>.
list_dirList the entries in a directory.
Returns a list of entry names (not full paths). Returns an empty list on
error. Use try_list_dir when missing-path handling matters.
let entries = fs.list_dir("/tmp");
for entry in entries {
println(entry);
}
try_list_dirList the entries in a directory.
Distinguishes missing and non-directory paths from a successful listing.
renameRename or move a file or directory.
Returns 0 on success, -1 on error.
try_renameRename or move a file or directory.
Lifts the native status code into Result<i64, IoError>.
copyCopy a file.
Returns 0 on success, -1 on error.
try_copyCopy a file.
Lifts the native status code into Result<i64, IoError>.
is_dirCheck whether a path is a directory.
IoErrorStructured error type for file-system and network operations.
Define Result<T, IoError> return types in your own functions for
structured error handling with the ? operator.
Variants below cover both file-system and TCP/network failure codes so
callers can handle connection errors uniformly across fs, net, quic,
http, tls, dns, and websocket modules.
NotFound(i64)The target path does not exist.
PermissionDenied(i64)The process lacks permission for the operation.
AlreadyExists(i64)A file or directory already exists at the target path.
ConnectionRefused(i64)The remote host actively refused the connection (ECONNREFUSED).
AddressInUse(i64)The local address is already in use (EADDRINUSE).
TimedOut(i64)The connection attempt or I/O operation timed out (ETIMEDOUT).
Cancelled(i64)Operation was cancelled (e.g., a deadline expired before the I/O could complete).
Distinct from TimedOut: TimedOut means the deadline expired; Cancelled
means the operation was abandoned for another reason (e.g. the blocking
pool was shut down mid-call).
AddressNotAvailable(i64)The address or hostname could not be resolved (EADDRNOTAVAIL).
Other(i64)Any other OS-level I/O failure.
FileReadStreamOpaque stream handle returned by stream_from_file_read.