Standard Library
The Ferret standard library provides a rich set of modules for common programming tasks, from I/O operations to HTTP servers.
Core Modules
Section titled “Core Modules”Built-in Functions
Section titled “Built-in Functions”Global functions available without imports:
len()- Get length of sequencesappend()- Append to arraysat()- Safe indexingsize()- Get map sizeget()- Safe map accessset()- Set map values
Standard Library
Section titled “Standard Library”std/io- Input/output operationsstd/fs- Filesystem operationsos- Operating system interfacemath- Mathematical functionstime- Time and date utilitiesrandom- Random number generationnet/http- HTTP server and clientnet/tcp- TCP networkingdb/redis- Redis database client
Global Functions Reference
Section titled “Global Functions Reference”fn len(value: SequenceView) -> i32Returns the length of arrays, slices, or strings.
let arr := [1, 2, 3, 4, 5];let count := len(&arr); // 5
let text := "hello";let chars := text as []char;let length := len(&chars); // 5append()
Section titled “append()”fn append(source: SequenceRef, value: Type) -> boolAppends a value to the end of an array. Returns true on success.
let numbers := [1, 2, 3];append(&mut numbers, 4);// numbers is now [1, 2, 3, 4]fn at(value: SequenceView, index: i32) -> ?TypeSafely accesses an element by index. Returns none if index is out of bounds.
let arr := ["a", "b", "c"];let first := at(&arr, 0); // some("a")let invalid := at(&arr, 10); // nonesize()
Section titled “size()”fn size(value: MapView) -> i32Returns the number of key-value pairs in a map.
let ages := {"alice": 25, "bob": 30};let count := size(&ages); // 2fn get(value: MapView, key: Type1) -> ?Type2Safely retrieves a value from a map. Returns none if key doesn’t exist.
let scores := {"player1": 100, "player2": 85};let p1Score := get(&scores, "player1"); // some(100)let p3Score := get(&scores, "player3"); // nonefn set(source: MapRef, key: Type1, value: Type2) -> boolSets a key-value pair in a map. Returns true on success.
let config := {"debug": true};set(&mut config, "port", 8080);// config is now {"debug": true, "port": 8080}Module Import Patterns
Section titled “Module Import Patterns”Standard Modules
Section titled “Standard Modules”import "std/io"; // I/O operationsimport "std/fs"; // Filesystemimport "os"; // OS interfaceimport "math"; // Math functionsNetwork Modules
Section titled “Network Modules”import "net/http"; // HTTP server/clientimport "net/tcp"; // TCP socketsDatabase Modules
Section titled “Database Modules”import "db/redis"; // Redis clientError Handling
Section titled “Error Handling”Most standard library functions return Result types (str ! Type) for error handling:
import "std/fs";
// Handle errors with matchlet content := match fs::ReadFile("config.txt") { ok(data) => data, err(msg) => { io::Println("Error:", msg); return; }};
// Or use catch for error propagationfn loadConfig() -> str ! map[str]str { let raw := fs::ReadFile("app.config") catch err err; // parse and return config...}Type System Integration
Section titled “Type System Integration”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
?Typeinstead of null - References: Functions accept both owned values and references
// Generic append works with any typelet names := ["Alice"];let numbers := [1, 2];
append(&mut names, "Bob"); // Works with stringsappend(&mut numbers, 3); // Works with integers
// Union types for flexible APIsimport "std/io";io::Println("Count:", 42, true, 3.14); // Mixed typesNext Steps
Section titled “Next Steps”- Explore individual modules: I/O, Filesystem, HTTP
- Learn about Error Handling
- See Practical Examples using the standard library