Module std::fs

File system operations.

Read, write, and query files on the local file system.

Examples

import std::fs;

fn main() {
    let contents = fs.read("config.toml");
    fs.write("output.txt", "hello");
    if fs.exists("data.csv") {
        let size = fs.size("data.csv");
        println(size);
    }
}

Contents

Functions

Function read

pub fn read(path: String) -> String

Read the entire contents of a file as a UTF-8 string.

Panics if the file does not exist or cannot be read.

Examples

let src = fs.read("main.hew");

Function write

pub fn write(path: String, content: String) -> i32

Write a string to a file, creating or overwriting it.

Returns 0 on success, non-zero on error.

Function append

pub fn append(path: String, content: String) -> i32

Append a string to the end of a file.

Creates the file if it does not exist. Returns 0 on success.

Function exists

pub fn exists(path: String) -> bool

Check whether a file exists at the given path.

Examples

if fs.exists("config.toml") {
    println("found config");
}

Function delete

pub fn delete(path: String) -> i32

Delete a file. Returns 0 on success.

Function size

pub fn size(path: String) -> i64

Returns the size of a file in bytes.

Function read_line

pub fn read_line() -> String

Read a single line from standard input.

Blocks until the user presses Enter.

Examples

print("Name: ");
let name = fs.read_line();

Function read_bytes

pub fn read_bytes(path: String) -> bytes

Read the raw bytes of a file into a bytes value.

Returns an empty bytes buffer on error.

Examples

let data = fs.read_bytes("image.png");
println(data.len());

Function write_bytes

pub fn write_bytes(path: String, data: bytes) -> i32

Write a bytes value to a file, creating or overwriting it.

Returns 0 on success, non-zero on error.

Function mkdir

pub fn mkdir(path: String) -> i32

Create 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.

Function mkdir_all

pub fn mkdir_all(path: String) -> i32

Create a directory and all its parent components.

Returns 0 on success, -1 on error.

Function list_dir

pub fn list_dir(path: String) -> Vec<String>

List the entries in a directory.

Returns a list of entry names (not full paths). Returns an empty list on error.

Examples

let entries = fs.list_dir("/tmp");
for entry in entries {
    println(entry);
}

Function rename

pub fn rename(from: String, to: String) -> i32

Rename or move a file or directory.

Returns 0 on success, -1 on error.

Function copy

pub fn copy(from: String, to: String) -> i32

Copy a file.

Returns 0 on success, -1 on error.

Function is_dir

pub fn is_dir(path: String) -> bool

Check whether a path is a directory.