Module 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();