Nabi 0.1.3 已于 2026年11月22日 发布 →
规则清晰,
规则清晰,
如蝶般自由。
一门编译型语言:封闭的和类型,没有 null,错误写在签名里。
编译器替你检查什么
和类型的每一种情况
一个值的类型是 i32 | str | bool,match 就必须处理这三种情况。漏掉一种,或者以后再加一种,编译器都会指出哪种情况没有分支处理。
src/main.nabi运行 ↗
fn describe(v: i32 | str | bool) -> str {
match v {
n: i32 => f"integer {n}",
s: str => f"string {s}",
}
}
fn main() {
println(describe(true));
}$ nabi run Compiling describe (.) error[E0098]: non-exhaustive match on `bool | i32 | str`: `bool` not covered --> src/main.nabi:2:5 | 2 | match v { | ^^^^^^^^^ | = help: add `x: bool => ..` error: could not build describe (1 error, 0 warnings)
src/main.nabi运行 ↗
fn find(xs: List<i32>, target: i32) -> usize? {
for i in 0..xs.len() {
if xs[i] == target { return i; }
}
none
}
fn main() {
let at: usize = find([70, 90], 90);
println(at);
}$ nabi run Compiling find (.) error[E0040]: mismatched types: `let at` expects `usize`, found `usize | none` --> src/main.nabi:9:21 | 9 | let at: usize = find([70, 90], 90); | ^^^^^^^^^^^^^^^^^^ | = help: `find([70, 90], 90)` may be none: write `find([70, 90], 90) ?? 0`, or `if let x = find([70, 90], 90) { .. }` error: could not build find (1 error, 0 warnings)
src/main.nabi运行 ↗
fn parse_age(s: str) throws -> i32 {
let n: i32 = try s.parse();
if n < 0 { throw f"age {n} is negative"; }
n
}
fn main() {
let age = parse_age("-1");
println(age);
}$ nabi run Compiling ages (.) error[E0062]: this call can throw `any Error` and must be marked with `try` (`try f()`; `try? f()` / `try! f()` to drop / panic on the error) --> src/main.nabi:8:15 | 8 | let age = parse_age("-1"); | ^^^^^^^^^^^^^^^ error: could not build ages (1 error, 0 warnings)
使用包
Nabi 项目发布的包随工具链一起安装:JSON 和 HTTP、表格、统计、图表等。写下 use 然后运行,nabi 就会把包加入清单,无需下载,也无需联网。注册表上的其他包,一条 nabi add <名称> 即可添加。
main.nabi
use json::parse;
let v = try parse("{\"lang\": \"nabi\", \"year\": 2026}");
println(v.get("lang")?.as_str() ?? "");$ nabi run main.nabi Added json ^0.1 (official) nabi
数独求解器在定长数组表示的棋盘上用回溯法求解数独,在每个空格中逐个尝试候选数字。生命游戏在边缘相互连通的 10×8 环面网格上运行 Conway 的生命游戏,并以文本输出几代。一个小型 Lisp 解释器构建一个小型 Lisp,包含词法分析器、递归下降解析器和带环境的求值器,支持 define、if、lambda 和闭包,并通过 throws 报告运行时错误。TCP 回显服务器一个程序在空闲的 127.0.0.1 端口上启动回显服务器,让客户端发送若干行并读回回显,最后干净地关闭所有连接。文字冒险游戏一个有房间、出口、物品和一扇锁着的门的小型文字冒险游戏,按一份预先写好的命令列表一直玩到结局。CSV 报表把一份小的销售 CSV 写入临时文件,再逐行读回,并输出一张列对齐的按地区汇总表。单词计数统计一段文本的行数、单词数和字符数,再用 Dict 和排序选出出现最多的五个单词。JSON 解析器用枚举表示 JSON 值,用一个字符级的递归下降解析器通过 throws 报告错误位置,再加上一个带缩进的美化输出器。
安装
一套工具链全包:自带代码生成器和链接器的编译器、包管理工具、REPL、Jupyter 内核、语言服务器和官方包。无需另外安装任何东西。
# macOS (Apple silicon), Linux, FreeBSD
curl -fsSL https://nabi.sh | sh
# Windows (x64, arm64), 在 PowerShell 中
powershell -ExecutionPolicy Bypass -c "irm https://nabi.sh/install.ps1 | iex"
# Nix
nix profile install github:nabi-lang/nabi| macOS | aarch64-apple-darwin | Apple 芯片 | .tar.xz24 MB |
|---|---|---|---|
| Windows | x86_64-pc-windows-msvc | x64,Windows 11 | .zip70 MB |
| Windows | aarch64-pc-windows-msvc | arm64,Windows 11 | .zip68 MB |
| Linux | x86_64-unknown-linux-gnu | glibc 2.34 及以上 | .tar.xz26 MB |
| Linux | aarch64-unknown-linux-gnu | glibc 2.34 及以上 | .tar.xz24 MB |
| Linux | x86_64-unknown-linux-musl | 静态链接,适用于任何发行版 | .tar.xz26 MB |
| Linux | aarch64-unknown-linux-musl | 静态链接,适用于任何发行版 | .tar.xz24 MB |
| FreeBSD | x86_64-unknown-freebsd | FreeBSD 14 及以上 | .tar.xz26 MB |
校验和、其他格式和交叉编译目标:下载。
nabi auditreadsnabi.lockand reports each pinned package that a packages.nabi-lang.org advisory affects, with the advisory ID, summary, severity and the version to update to (for examplenabi update json). It exits 0 when nothing is affected, 1 when something is, and 2 when it cannot check, so CI can run it as a gate.--jsonprints machine-readable output,--ignore <ID>skips an advisory, and--offlineuses only the cached advisory list. The list is cached for an hour; without a network, an older cache is used with a warning. Path and git dependencies are skipped and listed as not checked.