本页尚未翻译,以下为英文原文。
#Examples
A collection of small, complete Nabi 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 Nabi.
Bank transfersSeveral threads move money between accounts guarded by Mutex, collect statistics in Atomic counters, and finally check that the total never changed.Big integers and factorialsCompute 100!, the 300th Fibonacci number and powers beyond 128 bits with the arbitrary-precision int, and find where i64 overflows.Command-line toolmgrep, a small grep-like tool, parses its command-line arguments by hand into a struct and handles --help and bad arguments with throws.CSV reportWrite 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 downloaderA 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 libraryDeclare 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 bare.Game loopA 60 fps loop that keeps the garbage collector out of its frames: a pause goal, particles in a frame arena, drawing in a region that never collects, and a minor collection in the time left of each frame.Game of LifeRun Conway's Game of Life on a 10×8 torus grid whose edges wrap around, printing a few generations as text.GeneratorsWrite 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 interpreterBuild 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 parserJSON values as an enum, a character-level recursive-descent parser that reports error positions with throws, and an indenting pretty printer.Matrix operationsA small matrix type that implements the operator and index traits, so you can write a * b, a .* b and m[i, j].NABION configurationRead a NABION configuration into #[derive(Nabion)] structs with defaults filled in, validate it, change it and write it back as NABION.Exploring data in a Jupyter notebookInstall the Nabi kernel in Jupyter and explore temperature data cell by cell, alongside a single runnable program that does the same work.Parallel map / reduceSplit prime counting across cores with map, fold, filter and join from core::par, and check the result against a sequential count.Child processesRun external programs such as echo, a shell pipeline and tr with process::run and std::process::Command, and work with their output, exit codes, environment variables and standard input.Sealed messagesEncrypt a message so only its recipient can read it, and sign it so the recipient knows who sent it, with nothing but core::crypto and the OS random source.Sudoku solverSolve a sudoku with backtracking over a fixed-size array board, trying each candidate digit in every empty cell.TCP echo serverOne 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 adventureA small text adventure with rooms, exits, items and a locked door, played to the end from a prewritten list of commands.Word countCount the lines, words and characters of a text, then use a Dict and sorting to pick the five most frequent words.
Found a mistake? Edit this page on GitHub. Last updated