Examples
A collection of small, complete rok programs. Each example is a single file; every site build compiles and runs it and compares the result with the output shown on the page.
To learn the syntax step by step, start with Learn rok.
Bank transfers
Several threads move money between accounts guarded by Mutex, collect statistics in Atomic counters, and finally check that the total never changed.
Big integers and factorials
Compute 100!, the 300th Fibonacci number and powers beyond 128 bits with the arbitrary-precision int, and find where i64 overflows.
Command-line tool
mgrep, a small grep-like tool, parses its command-line arguments by hand into a struct and handles --help and bad arguments with throws.
CSV report
Write a small sales CSV to a temporary file, read it back line by line, and print a per-region summary table with aligned columns.
Concurrent downloader
A pool of worker tasks shares out simulated resources (latency faked with task::sleep instead of a network), results come back over a channel, and a sorted report is printed.
Calling a C library
Declare libc's strlen, getpid, getenv, snprintf and qsort in an extern "C" block, and manage C memory directly with the pointer and allocation functions of rok::bare.
Game of Life
Run Conway's Game of Life on a 10×8 torus grid whose edges wrap around, printing a few generations as text.
Generators
Write the Fibonacci sequence, a prime stream, a hand-made take_while adapter and a recursive tree walk as lazy iterators with yield fn.
A small Lisp interpreter
Build a small Lisp with a tokenizer, a recursive-descent parser and an evaluator that carries an environment, supporting define, if, lambda and closures, with runtime errors reported through throws.
JSON parser
JSON values as an enum, a character-level recursive-descent parser that reports error positions with throws, and an indenting pretty printer.
Matrix operations
A small matrix type that implements the operator and index traits, so you can write a * b, a .* b and m[i, j].
Exploring data in a Jupyter notebook
Install the rok kernel in Jupyter and explore temperature data cell by cell, alongside a single runnable program that does the same work.
Parallel map / reduce
Split prime counting across cores with map, reduce, filter and join from rok::core::par, and check the result against a sequential count.
Child processes
Run external programs such as echo, a shell pipeline and tr with rok::std::process::Command, and work with their output, exit codes, environment variables and standard input.
ROKON configuration
Read a ROKON configuration into #[derive(Rokon)] structs with defaults filled in, validate it, change it and write it back as ROKON.
Sealed messages
Encrypt a message so only its recipient can read it, and sign it so the recipient knows who sent it, with nothing but rok::core::crypto and the OS random source.
Sudoku solver
Solve a sudoku with backtracking over a fixed-size array board, trying each candidate digit in every empty cell.
TCP echo server
One program starts an echo server on a free 127.0.0.1 port, has clients send lines and read back the echoes, and then shuts everything down cleanly.
Text adventure
A small text adventure with rooms, exits, items and a locked door, played to the end from a prewritten list of commands.
Word count
Count the lines, words and characters of a text, then use a Dict and sorting to pick the five most frequent words.