Skip to content

Standard Library

The Ferret standard library provides a rich set of modules for common programming tasks, from I/O operations to HTTP servers.

Global functions available without imports:

  • std/io - Input/output operations
  • std/fs - Filesystem operations
  • os - Operating system interface
  • math - Mathematical functions
  • time - Time and date utilities
  • random - Random number generation
  • net/http - HTTP server and client
  • net/tcp - TCP networking
  • db/redis - Redis database client
fn len(value: SequenceView) -> i32

Returns the length of arrays, slices, or strings.

example
let arr := [1, 2, 3, 4, 5];
let count := len(&arr); // 5
let text := "hello";
let chars := text as []char;
let length := len(&chars); // 5
fn append(source: SequenceRef, value: Type) -> bool

Appends a value to the end of an array. Returns true on success.

example
let numbers := [1, 2, 3];
append(&mut numbers, 4);
// numbers is now [1, 2, 3, 4]
fn at(value: SequenceView, index: i32) -> ?Type

Safely accesses an element by index. Returns none if index is out of bounds.

example
let arr := ["a", "b", "c"];
let first := at(&arr, 0); // some("a")
let invalid := at(&arr, 10); // none
fn size(value: MapView) -> i32

Returns the number of key-value pairs in a map.

example
let ages := {"alice": 25, "bob": 30};
let count := size(&ages); // 2
fn get(value: MapView, key: Type1) -> ?Type2

Safely retrieves a value from a map. Returns none if key doesn’t exist.

example
let scores := {"player1": 100, "player2": 85};
let p1Score := get(&scores, "player1"); // some(100)
let p3Score := get(&scores, "player3"); // none
fn set(source: MapRef, key: Type1, value: Type2) -> bool

Sets a key-value pair in a map. Returns true on success.

example
let config := {"debug": true};
set(&mut config, "port", 8080);
// config is now {"debug": true, "port": 8080}
import "std/io"; // I/O operations
import "std/fs"; // Filesystem
import "os"; // OS interface
import "math"; // Math functions
import "net/http"; // HTTP server/client
import "net/tcp"; // TCP sockets
import "db/redis"; // Redis client

Most standard library functions return Result types (str ! Type) for error handling:

example
import "std/fs";
// Handle errors with match
let content := match fs::ReadFile("config.txt") {
ok(data) => data,
err(msg) => {
io::Println("Error:", msg);
return;
}
};
// Or use catch for error propagation
fn loadConfig() -> str ! map[str]str {
let raw := fs::ReadFile("app.config") catch err err;
// parse and return config...
}

Standard library modules fully integrate with Ferret’s type system:

  • Generics: Functions work with any compatible type
  • Union Types: Many functions accept flexible input types
  • Optional Types: Safe operations return ?Type instead of null
  • References: Functions accept both owned values and references
example
// Generic append works with any type
let names := ["Alice"];
let numbers := [1, 2];
append(&mut names, "Bob"); // Works with strings
append(&mut numbers, 3); // Works with integers
// Union types for flexible APIs
import "std/io";
io::Println("Count:", 42, true, 3.14); // Mixed types