docs

You are reading the docs for v0.1.2. Nabi 0.1.2 is no longer supported. Latest release: v0.1.3 →

#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

  1. 1
    Getting 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.

  2. 2
    Values and Types

    This chapter covers let for 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.

  3. 3
    Functions

    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.

  4. 4
    Control Flow

    This chapter covers branching (if, match), looping (while, for, loop), and destructuring. The key point is that if, match, and loop in Nabi are all expressions that produce values.

  5. 5
    Sum Types and none

    When a value is "either this or that", Nabi joins the types with |, as in A | 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".

  6. 6
    Structs, Enums, and Traits

    Now it's time to define your own types. This chapter covers struct, which groups data; enum, which lists named cases; and trait, which defines behavior shared by several types.

  7. 7
    Generics

    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.

  8. 8
    Collections and Iterators

    Most programs collect, traverse, and transform many values. This chapter covers the standard collections (List, Dict, Set, and their relatives), slicing, and the Iterator that ties them together.

  9. 9
    Strings and f-strings

    Strings show up in almost every program. This chapter covers str and StrBuf, the commonly used methods, the different kinds of literals, and the format specifiers of the f-strings you have been using all along.

  10. 10
    Error 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 throws in its signature, and the caller marks that call with try. As a result, every point where an error can flow is visible in the code.

  11. 11
    Modules 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.

  12. 12
    Concurrency

    The right way to do several things at once depends on the goal. Threads and par suit computations that use multiple CPUs, while async tasks suit I/O with lots of waiting. This chapter looks at each tool through short examples and also covers channels, Mutex, and Atomic for exchanging data safely.

  13. 13
    File 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.

  14. 14
    FFI 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.

  15. 15
    NABION

    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.nabion uses this format too.

  16. 16
    Tools

    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 nabi command.

  17. 17
    Conditional 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.

  18. 18
    Features 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.

  19. 19
    Cryptography and TLS

    Nabi ships its own cryptography. core::crypto has the algorithms (hashes, HMAC and HKDF, authenticated encryption, key exchange, signatures), std::random reads the operating system's secure random source, and std::net::tls is 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.

  20. 20
    How 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.

  21. 21
    Arrays 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.

  22. 22
    Science with Nabi

    Nabi's numeric core is in the standard library: Array<T, N>, the dot operators, reductions, Complex and 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.

  23. 23
    Plugins

    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, any values, is a normal value of your program, and you call its functions like any other function.

  24. 24
    Reflection

    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.

  25. 25
    Garbage 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::gc that keep them short or out of the way.

  26. 26
    Hot Reload

    nabi dev runs 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.

  27. 27
    Objective-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::import reads 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.

  28. 28
    iOS

    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.

  29. 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.

  30. 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