File System (std::fs)
std::fs touches the disk: reading, writing, deleting, listing directories, and
temporary paths. Because all operations interact with the host file system, they
cannot be run in the browser WASM sandbox; run them with the Zolo CLI.
Read and write files
fs.write(path, content) writes text; fs.read(path) loads the content back
as a string; fs.remove(path) deletes the file. The examples below use
fs.temp_dir() to avoid polluting the repository:
Creates, reads, and deletes a text file in the system's temporary directory.
// Feature: fs.write / fs.read — simple round-trip
// When to use: persist/load text on disk. Note: we use fs.temp_dir()
// to write to the system temp area and avoid littering the repo.
use std::fs
let dir = fs.temp_dir()
let path = dir + "/zolo-fs-roundtrip.txt"
let content = "hello, zolo!\nline 2"
fs.write(path, content)
// Read it back.
let loaded = fs.read(path)
print(loaded == content) // expected: true
print(loaded.contains("zolo")) // expected: true
// Cleanup.
fs.remove(path)
print(fs.exists(path)) // expected: false
Requires the Zolo CLI/host — open in the playground or run locally.
Check existence and type
fs.exists distinguishes existing paths from non-existing ones. fs.is_file
and fs.is_dir differentiate regular files from directories:
Demonstrates exists, is_file, and is_dir before and after creating a file.
// Feature: fs.exists / fs.is_file / fs.is_dir — existence checks
// When to use: verify before reading/writing, distinguish kind.
use std::fs
let dir = fs.temp_dir()
let path = dir + "/zolo-fs-exists.txt"
// Before creating — does not exist.
print(fs.exists(path)) // expected: false
fs.write(path, "hello")
print(fs.exists(path)) // expected: true
print(fs.is_file(path)) // expected: true
print(fs.is_dir(path)) // expected: false
// temp_dir itself is a directory.
print(fs.exists(dir)) // expected: true
print(fs.is_dir(dir)) // expected: true
print(fs.is_file(dir)) // expected: false
// Cleanup.
fs.remove(path)
print(fs.exists(path)) // expected: false
Requires the Zolo CLI/host — open in the playground or run locally.
List directories
fs.list(dir) returns an array of directory entries. fs.mkdir creates a
directory; fs.rmdir removes an empty directory:
Creates 3 files in a temporary subdirectory, lists and counts the .txt entries.
// Feature: fs.list — list contents of a directory
// When to use: walk files in a folder, generate indexes.
use std::fs
// Create 3 files inside a temp sub-directory to list.
let base = fs.temp_dir() + "/zolo-fs-list"
fs.mkdir(base)
fs.write(base + "/a.txt", "1")
fs.write(base + "/b.txt", "2")
fs.write(base + "/c.txt", "3")
// list returns an array of paths/entries.
let entries = fs.list(base)
print(entries.len() >= 3) // expected: true
// Count how many end with .txt.
var txts = 0
for e in entries {
if e.ends_with(".txt") {
txts = txts + 1
}
}
print(txts >= 3) // expected: true
// Cleanup.
fs.remove(base + "/a.txt")
fs.remove(base + "/b.txt")
fs.remove(base + "/c.txt")
fs.rmdir(base)
print(fs.exists(base)) // expected: false
Requires the Zolo CLI/host — open in the playground or run locally.
Temporary files and directories
fs.temp_dir() returns the system's temporary directory. fs.temp_file(prefix)
reserves a unique name that does not exist yet, suitable for draft files, caches,
and tests:
Uses temp_dir and temp_file to create and delete a draft file.
// Feature: fs.temp_dir / fs.temp_file — temporary scratch areas
// When to use: caches, scratch, tests — without polluting the project tree.
use std::path
use std::fs
// temp_dir — path to the system temp directory.
let dir = fs.temp_dir()
print(fs.is_dir(dir)) // expected: true
print(dir.len() > 0) // expected: true
// temp_file(prefix) — a unique unused path.
let path = fs.temp_file("zolo-demo-")
print(path.contains("zolo-demo-")) // expected: true
print(fs.exists(path)) // expected: false (only the name is reserved)
// Write and remove.
fs.write(path, "scratch")
print(fs.exists(path)) // expected: true
print(fs.read(path)) // expected: scratch
fs.remove(path)
print(fs.exists(path)) // expected: false
Requires the Zolo CLI/host — open in the playground or run locally.
See also