Standard Library
Built-in types: String, Vec, HashMap, Iterator, IO.
Three-tier architecture
Hew's standard library is organized into three tiers, enabling use in environments ranging from bare-metal to full OS:
- core — No allocation, no OS. Primitive types,
Option<T>,Result<T, E>, Iterator trait, marker traits (Send,Frozen,Copy), and memory intrinsics. Works on bare metal. - alloc — Heap allocation, no OS.
Vec<T>,String,Box<T>,Arc<T>,Rc<T>,HashMap<K, V>,HashSet<T>, and formatting. Works anywhere with a heap. - std — Full OS integration. File system (
std::fs), networking (std::net), IO (std::io), environment (std::env), and process spawning (std::process).
String
String is an owned, heap-allocated, growable UTF-8 string. Internally it wraps a Vec<u8> that is guaranteed to contain valid UTF-8.
struct String { // Internal: Vec<u8> guaranteed valid UTF-8}impl String { fn new() -> String; fn from(s: str) -> String; fn len(self) -> usize; fn push(self, c: char); fn push_str(self, s: str); fn as_str(self) -> str;}Vec<T>
Vec<T> is a growable, heap-allocated array. It manages a contiguous block of memory with a length and capacity.
impl<T> Vec<T> { fn new() -> Vec<T>; fn with_capacity(cap: usize) -> Vec<T>; fn push(self, item: T); fn pop(self) -> Option<T>; fn len(self) -> usize; fn get(self, index: usize) -> Option<T>; fn truncate(self, len: usize); fn clone(self) -> Vec<T>; fn swap(self, a: usize, b: usize); fn sort(self) where T: Ord;}// Default: uses actor's current allocatorlet items = Vec::new();// Explicit: uses provided allocatorlet temp = Vec::new_in(arena_allocator);HashMap<K, V>
HashMap<K, V> is a hash table using Robin Hood hashing. Keys must implement Hash + Eq.
impl<K: Hash + Eq, V> HashMap<K, V> { fn new() -> HashMap<K, V>; fn insert(self, key: K, value: V) -> Option<V>; fn get(self, key: K) -> Option<V>; fn remove(self, key: K) -> Option<V>; fn contains_key(self, key: K) -> bool;}Iterator
The Iterator trait provides lazy, composable data transformation with combinators like map, filter, fold, and collect.
trait Iterator { type Item; fn next(self) -> Option<Self::Item>; // Provided combinators fn map<B>(self, f: fn(Self::Item) -> B) -> Map<Self, B>; fn filter(self, pred: fn(Self::Item) -> bool) -> Filter<Self>; fn collect<C: FromIterator<Self::Item>>(self) -> C; fn fold<B>(self, init: B, f: fn(B, Self::Item) -> B) -> B;}trait IntoIterator { type Item; type IntoIter: Iterator<Item = Self::Item>; fn into_iter(self) -> Self::IntoIter;}IO
The IO system is built on Read and Write traits. All IO operations return Result — there is no implicit blocking.
trait Read { fn read(self, buf: [u8]) -> Result<usize, IoError>; fn read_exact(self, buf: [u8]) -> Result<(), IoError>; fn read_to_end(self, buf: Vec<u8>) -> Result<usize, IoError>; fn read_to_string(self, buf: String) -> Result<usize, IoError>;}trait Write { fn write(self, buf: [u8]) -> Result<usize, IoError>; fn flush(self) -> Result<(), IoError>; fn write_all(self, buf: [u8]) -> Result<(), IoError>;}trait BufRead: Read { fn fill_buf(self) -> Result<[u8], IoError>; fn consume(self, amt: usize); fn read_line(self, buf: String) -> Result<usize, IoError>;}The File type wraps a file descriptor with safe open/close semantics using Drop for deterministic resource cleanup:
pub struct File { fd: i32 }impl File { pub fn open(path: String) -> Result<File, IoError> { let c_path = path.to_c_string(); let fd = unsafe { open(c_path.as_ptr(), O_RDONLY) }; if fd < 0 { Err(IoError::from_errno()) } else { Ok(File { fd }) } }}impl Drop for File { fn drop(self) { unsafe { close(self.fd); } }}// File I/O convenience functionsfn read_file(path: String) -> Result<String, IoError>;fn write_file(path: String, content: String) -> Result<(), IoError>;Allocators
Hew provides an explicit allocator interface for fine-grained memory control. Collections can be parameterized over allocators.
- GlobalAllocator — Default system allocator
- ArenaAllocator — Bump allocation with O(1) bulk deallocation
- PoolAllocator — Fixed-size object pools for uniform allocations
trait Allocator { fn alloc(self, size: usize, align: usize) -> Result<*mut u8, AllocError>; fn dealloc(self, ptr: *mut u8, size: usize, align: usize);}