Skip to content

Language Reference

yuzhe edited this page Mar 12, 2026 · 1 revision

Language Reference

Types

Type Description Example
int Integer (MC scoreboard) let x: int = 42;
string Text literal let s: string = "hello";
bool Boolean let b: bool = true;
selector MC entity selector @s, @a[distance=..5]
BlockPos 3D coordinate (10, 64, -5), (~1, ~0, ~-1)
T[] Array (NBT storage) let arr: int[] = [];

Variables

let x: int = 10;
let name: string = "Player";
let pos: BlockPos = (0, 64, 0);
let rel: BlockPos = (~0, ~1, ~0);   // relative (~)
let loc: BlockPos = (^0, ^1, ^0);   // local (^)

// Constants (compile-time inlined)
const MAX_PLAYERS: int = 8;
const SPAWN: BlockPos = (0, 64, 0);

Functions

fn greet(player: selector, msg: string = "Hello!") {
    tell(player, msg);
}

// Return values
fn add(a: int, b: int) -> int {
    return a + b;
}

// Calling
greet(@s);
greet(@a, "Welcome back!");
let sum: int = add(3, 4);

Control Flow

if / else

if (x > 10) {
    say("big");
} else if (x == 10) {
    say("ten");
} else {
    say("small");
}

for

for (let i: int = 0; i < 5; i = i + 1) {
    scoreboard_set(@s, "count", i);
}

// foreach over selector
foreach (player in @a) {
    heal(player, 5);
}

while

let n: int = 10;
while (n > 0) {
    n = n - 1;
}

match

match (state) {
    0 => { announce("Idle"); }
    1 => { announce("Running"); }
    _ => { announce("Unknown"); }
}

Structs

struct Player {
    name: string,
    score: int,
    alive: bool,
}

let p = Player { name: "Steve", score: 0, alive: true };
p.score = p.score + 10;

Enums

enum GamePhase {
    Lobby,
    Playing,
    Ended,
}

let phase = GamePhase::Playing;

Arrays

Backed by NBT storage (data modify storage rs:heap).

let nums: int[] = [];
push(nums, 42);
push(nums, 100);
let len: int = len(nums);      // 2
let first: int = nums[0];      // 42
pop(nums);                     // removes last

foreach (n in nums) {
    scoreboard_set(@s, "val", n);
}

Lambdas

Lambdas are monomorphized (inlined at call site). No runtime function pointers.

fn apply(f: (int) -> int, x: int) -> int {
    return f(x);
}

let double = (x: int) -> int { return x * 2; };
let result = apply(double, 5);  // → 10

// Captures (read-only outer vars)
let base: int = 100;
let adder = (x: int) -> int { return x + base; };

Note: Capturing mutable outer variables is a compile error.

Decorators

// Runs every server tick
@tick
fn on_tick() { ... }

// Runs every N ticks
@tick(rate=20)
fn every_second() { ... }

// Fires when a player earns an advancement
@on_advancement("story/mine_diamond")
fn on_diamond() { ... }

// Fires on player death
@on_death
fn on_player_death() { ... }

// Fires when a player crafts an item
@on_craft("minecraft:diamond_sword")
fn on_craft_sword() { ... }

Advancement-based decorators compile to data/advancement/ JSON files in addition to the function.

String Interpolation

let name: string = "Steve";
let score: int = 42;
announce("Player ${name} has ${score} points");

Imports

import "stdlib/math.rs"
import "../shared/utils.rs"

Import paths are resolved relative to the source file. Circular imports are detected and rejected.

Selectors

Selectors are a first-class type. The compiler validates entity type names and automatically qualifies unnamespaced types.

// Single-target selectors: @s @p @r or @e/@a with limit=1
let target: selector = @s;

// Multi-target
kill(@e[type=minecraft:zombie]);
tell(@a[distance=..10], "You're nearby");

Raw Commands

Drop down to raw MC commands when needed:

fn do_effect() {
    raw "effect give @s minecraft:speed 10 1 true";
}

Scoping

  • Variables are function-scoped
  • Selector expressions have isSingle metadata for type-safe tp calls
  • Scoreboard objectives must exist before use (the rs objective is auto-created by __load)

Clone this wiki locally