#Learn Nabi
On this page
A guide for newcomers to Nabi, from installation through error handling, concurrency, FFI and C sources, NABION, tools, packages, cryptography, a cookbook, arrays and numerics, the science packages, reflection, and garbage collector pauses. Each chapter has a short explanation and code you can run right away.
Every code block in this guide is compiled and run with the real toolchain when the site is built, and its output is checked against the one shown. Copy any of them and run it with nabi run file.nabi.
Coming from Python? Nabi for Pythonistas maps Python idioms and standard library modules to Nabi.
Coming from Rust? Nabi for Rustaceans shows what to unlearn about ownership, errors and Option, and where Cargo's commands live.
Doing science or data analysis? Science with Nabi maps the science packages, and each package's docs start with a tutorial, from a first CSV analysis to linear algebra, statistics, signals, fitting, differential equations and derivatives. The Rosetta pages solve well-known small tasks (FizzBuzz, a prime sieve, eight queens, word counts) in Nabi.
The complete rules are defined in the language reference and the standard library specification; functions and types are listed in the standard library API. Small complete programs are collected in Examples. For quick answers to everyday tasks (reading a file, timing, threads, JSON, HTTP), see How Do I.... When the compiler stops you, look its code up in the error code index.
#Contents
- 1Getting Started
In this chapter you install the nabi toolchain, create and run your first package, and try a single-file script and the REPL. By the end you can run the examples in the rest of the guide yourself.
- 2Values and Types
This chapter covers
letfor declaring variables, basic types such as numbers and strings, and tuples and lists for grouping values. Nabi is statically typed, but most types are inferred, so you rarely write them out. - 3Functions
This chapter covers declaring and calling functions, default values and named arguments, and closures you can pass around as values. In Nabi, a function body is also a block expression, so its last expression is the return value.
- 4Control Flow
This chapter covers branching (
if,match), looping (while,for,loop), and destructuring. The key point is thatif,match, andloopin Nabi are all expressions that produce values. - 5Sum Types and none
When a value is "either this or that", Nabi joins the types with
|, as inA | B. This chapter covers sum types and the tools for working with the most common one,T | none, which means "there may be no value". - 6Structs, Enums, and Traits
Now it's time to define your own types. This chapter covers
struct, which groups data;enum, which lists named cases; andtrait, which defines behavior shared by several types. - 7Generics
Use generics when you want the same logic to work for many types. Nabi generics are compiled separately for each concrete type they're called with (monomorphization), so they run as fast as hand-written per-type functions.
- 8Collections and Iterators
Most programs collect, traverse, and transform many values. This chapter covers the standard collections (
List,Dict,Set, and their relatives), slicing, and theIteratorthat ties them together. - 9Strings and f-strings
Strings show up in almost every program. This chapter covers
strandStrBuf, the commonly used methods, the different kinds of literals, and the format specifiers of the f-strings you have been using all along. - 10Error Handling
Things that can fail, like a missing file or bad input, are everywhere. Nabi has a single error model. A function that can throw writes
throwsin its signature, and the caller marks that call withtry. As a result, every point where an error can flow is visible in the code. - 11Modules and Packages
As a program grows, you need to split the code up and use libraries written by other people. In Nabi, the unit for dividing code is the module, and the unit for building and distributing is the package.
- 12Concurrency
The right way to do several things at once depends on the goal. Threads and
parsuit computations that use multiple CPUs, whileasynctasks suit I/O with lots of waiting. This chapter looks at each tool through short examples and also covers channels,Mutex, andAtomicfor exchanging data safely. - 13File and Network IO
This chapter covers how a program talks to the world outside it. Files and directories, paths, standard IO, environment variables, child processes, and TCP / UDP sockets all live in
std. - 14FFI Basics
This chapter shows how to call functions written in C from Nabi, how to hand Nabi functions to C, and how to compile C code with Nabi's own C compiler. Operating systems and countless libraries expose their functionality through the C ABI, so knowing FFI (foreign function interface) lets you use features the standard library does not provide.
- 15NABION
This chapter shows how to read and write configuration files in NABION (Nabi Object Notation), Nabi's data format. There is no new syntax to learn: a NABION file uses the same literal syntax you write values with in Nabi source. The package manifest
nabi.nabionuses this format too. - 16Tools
This chapter tours the tools you keep at hand while writing code: the formatter, the linter, tests and benchmarks, documentation generation, editor support (LSP), the REPL and Jupyter, and debug options for looking inside the compiler. All of them start from the single
nabicommand. - 17Conditional Compilation
One source tree often has to build in several shapes: per operating system, debug against release, with or without an optional feature. Nabi decides this at compile time with
#[cfg(..)]. Code whose condition is false is removed right after parsing, as if it had never been written, so it costs nothing at run time and may even refer to names that do not exist in that build. - 18Features and Publishing
Chapter 11 built a package and added dependencies. This chapter goes further: features, which let the users of a library turn parts of it on and off; the packages the Nabi project maintains; and publishing your own library to the package registry, packages.nabi-lang.org.
- 19Cryptography and TLS
Nabi ships its own cryptography.
core::cryptohas the algorithms (hashes, HMAC and HKDF, authenticated encryption, key exchange, signatures),std::randomreads the operating system's secure random source, andstd::net::tlsis a TLS 1.3 client and server built on the two. No OpenSSL or other system library is involved, so the same code behaves the same on every target. - 20How Do I...
Short answers to everyday tasks, each a complete program you can paste into a file and run with
nabi run file.nabi. Each recipe links to the chapter that explains it. - 21Arrays and numerics
Numeric work in Nabi has three layers. Plain numbers have the usual operators plus
**for powers.List<T>and fixed arrays take the element-wise dot operators (.+,.*, ...) from chapter 8. For data with more than one axis,core::array::Array<T, N>is an n-dimensional array with NumPy-style indexing, slicing and broadcasting, built into core so that every library that does linear algebra, statistics or plotting can share one type. - 22Science with Nabi
Nabi's numeric core is in the standard library:
Array<T, N>, the dot operators, reductions,Complexand random numbers (chapter 21). Everything a scientist builds on top of that, linear algebra, statistics, signals, data frames, plots, lives in packages: one package per field, each versioned on its own, all written in Nabi with no C or Fortran underneath, so they build for every target Nabi has, WebAssembly included. - 23Plugins
A plugin is a package your program loads while it runs: game levels, editor tools, filters a user drops into a folder. In Nabi a plugin is an ordinary library package built with one more line in its manifest. It runs on your program's runtime, so everything it returns, strings, lists, closures,
anyvalues, is a normal value of your program, and you call its functions like any other function. - 24Reflection
Nabi programs can look at their own types while they run: the fields of a struct with their types, an enum's variants, and attributes you attach for tools. This is what an editor's property panel, a save-game system or a generic serializer is built on. This chapter shows how to ask for a type's description, read and change fields through it, attach your own attributes, and save and load a whole scene with no hand-written code per type.
- 25Garbage collector pauses
Nabi frees memory with a garbage collector, and most programs never think about it. A program with a time budget, a game drawing 60 frames a second, an audio callback, a server answering within a few milliseconds, wants to know when the collector stops it and how to keep those stops out of the part that must not wait. This chapter shows how the pauses come about, how to watch them, and the controls in
bare::gcthat keep them short or out of the way. - 26Hot Reload
nabi devruns your program and applies your edits while it runs. Save a file, and a few moments later the running program uses the new code, with everything it had built up still there: the level you were playing, the window you were tuning, the data you loaded. It is made for programs you watch while you work on them, such as games, simulations and tools with a user interface. - 27Objective-C and Apple Frameworks
On macOS the system's own libraries, Foundation, AppKit, Metal and the rest, are Objective-C classes. Nabi calls them directly: you declare the classes and methods you use in an
extern "objc"block, and each method becomes an ordinary Nabi method that sends the Objective-C message. No C compiler or glue code is involved. For Apple's frameworks you rarely write those blocks yourself:ffi::objc::importreads the SDK's headers and writes them (Importing the SDK's headers). The first sections declare by hand, which shows what an import gives you. - 28iOS
Nabi builds for the iOS simulator on Apple silicon Macs (
aarch64-apple-ios-sim). Builds for iPhones and iPads (aarch64-apple-ios) are in progress: that target is not tested on a device yet and comes in a later release, so for now an app with Nabi code runs in the simulator. An app's screens are Swift, so the Nabi part is a library that Xcode links in, and Swift calls it through the C ABI. Command-line programs also run on the simulator, which is how Nabi's own tests run there. - Nabi for Pythonistas
A map for Python programmers: what carries over, what Nabi writes differently, and where the Python standard library's modules live. Every block below is a complete program; run it with
nabi run file.nabi. The chapters of this guide explain each part in full. - Nabi for Rustaceans
A map for Rust programmers: what carries over, what to unlearn, and where Cargo's tools live. Much of Nabi's surface looks like Rust, but its memory model does not: memory is garbage collected, so there are no lifetimes and no borrow checker. Every block below is a complete program; run it with
nabi run file.nabi. The chapters of this guide explain each part in full.
Found a mistake? Edit this page on GitHub. Last updated