Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::fs

ItemSummary
readReturns the contents of the file at path as a string
writeWrites contents to the file at path, replacing anything the file held before, and returns true
list_dirReturns the paths of the files and directories directly inside the directory at path
walkReturns the path of every file and directory under path at any depth, starting with path itself
removeDeletes the file at path and returns true
remove_dirDeletes the directory at path and everything inside it, then returns true
make_dirCreates a directory at path, along with any parent directories that are missing, and returns true
cwdReturns the absolute path of the current working directory

Functions

read

fn read(path: str) -> str!

Returns the contents of the file at path as a string. Raises if the file can’t be read, such as when it doesn’t exist or isn’t valid UTF-8.

use std::fs;

let notes = fs::read("notes.txt")!;
let config = fs::read("config.txt") absolve |err| "";

write

fn write(path: str, contents: str) -> bool!

Writes contents to the file at path, replacing anything the file held before, and returns true. The file is created if it doesn’t exist, but the directory it goes in has to exist already (see make_dir). Raises if the file can’t be written.

use std::fs;

fs::write("scores.txt", "ada 3\nbob 5\n")!;

list_dir

fn list_dir(path: str) -> [str]!

Returns the paths of the files and directories directly inside the directory at path. Each path starts with path itself, and their order depends on the operating system. Raises if the directory can’t be read.

use std::fs;

for entry in fs::list_dir("saves")! {
    print(entry); // saves/slot1, saves/slot2, ...
}

walk goes into subdirectories as well.

walk

fn walk(path: str) -> [str]!

Returns the path of every file and directory under path at any depth, starting with path itself. A directory’s contents come right after it, and their order within the directory depends on the operating system. Raises if anything under path can’t be read.

use std::fs;

for path in fs::walk("assets")! {
    if path.ends_with(".png") {
        print(path);
    }
}

remove

fn remove(path: str) -> bool!

Deletes the file at path and returns true. Raises if it can’t, including when path is a directory (remove_dir deletes those).

use std::fs;

fs::remove("scores.txt")!;

remove_dir

fn remove_dir(path: str) -> bool!

Deletes the directory at path and everything inside it, then returns true. Raises if it can’t, such as when the directory doesn’t exist.

use std::fs;

fs::remove_dir("saves")!;

make_dir

fn make_dir(path: str) -> bool!

Creates a directory at path, along with any parent directories that are missing, and returns true. A directory that already exists is left as it is. Raises if the directory can’t be created.

use std::fs;

fs::make_dir("saves/slot1")!;
fs::write("saves/slot1/state.txt", "level 3")!;

cwd

fn cwd() -> str!

Returns the absolute path of the current working directory. Raises if it can’t be read, such as when the directory has been deleted.

Relative paths given to std::fs start from this directory, which is where the program was launched from. That isn’t necessarily where the script is: std::sys::file() gives the script’s own path.

use std::fs;

print(fs::cwd()!); // /home/ada/projects/game