Grundlagen
Hello World
Das kleinste vollständige Hew-Programm: ein Einstiegspunkt und eine Ausgabezeile.
Im Playground öffnenhello_world.hew
fn main() {
println("Hello, World!");
}Starten Sie mit kleinen, fokussierten Programmen, die zeigen, was der aktuelle Compiler und Playground behandeln können: Grundfunktionen, Actors, Supervision-Syntax und Wire-Schemas.
Das kleinste vollständige Hew-Programm: ein Einstiegspunkt und eine Ausgabezeile.
Im Playground öffnenfn main() {
println("Hello, World!");
}Rekursive Funktionen, Integer-Kontrollfluss, Ranges und formatierte Ausgabe.
Im Playground öffnenfn fib(n: i64) -> i64 {
if n <= 1 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}
fn main() {
for i in 0 .. 10 {
println(f"fib({i}) = {fib(i)}");
}
}Ein lokaler Actor besitzt seinen Zustand und ändert ihn über typisierte receive-Methoden.
Im Playground öffnenactor Counter {
var count: i64;
receive fn increment(n: i64) -> i64 {
count = count + n;
println(f"Count is now: {count}");
count
}
}
fn main() {
let c = spawn Counter { count: 0 };
// Each call waits for its handler; expect reports a failed call.
c.increment(5).expect("counter stopped");
c.increment(3).expect("counter stopped");
let total = match c.increment(12) {
.Ok(v) => v,
.Err(_) => -1,
};
println(f"Final total: {total}");
}Einen Actor starten und eine Folge von Nachrichtenaufrufen aus main erwarten.
Im Playground öffnenactor Doubler {
receive fn process(n: i64) -> i64 {
let result = n * 2;
println(f"Result: {result}");
result
}
}
fn main() {
let doubler = spawn Doubler;
let a = match doubler.process(5) {
.Ok(v) => v,
.Err(_) => -1,
};
let b = match doubler.process(21) {
.Ok(v) => v,
.Err(_) => -1,
};
let c = match doubler.process(100) {
.Ok(v) => v,
.Err(_) => -1,
};
println(f"got {a}, {b}, {c}");
}Ein kleiner Supervision-Tree mit Neustartstrategie, Budget und benannten Children.
Im Playground öffnenactor Worker {
let id: i64;
receive fn work(x: i64) -> i64 {
println(f"Worker {id} processing {x}");
x
}
}
supervisor WorkerPool {
strategy: one_for_one;
intensity: 5 within 10s;
child w1: Worker { id: 1 };
child w2: Worker { id: 2 };
}
fn main() {
let workers = spawn WorkerPool;
// Access supervised children by their declared names.
let w1 = workers.w1;
let w2 = workers.w2;
let a = match w1.work(10) {
.Ok(v) => v,
.Err(_) => -1,
};
let b = match w2.work(20) {
.Ok(v) => v,
.Err(_) => -1,
};
println(f"w1 -> {a}");
println(f"w2 -> {b}");
}
// After both replies, the program exits and its supervised workers stop.Getaggte Wire-Structs und Enums für explizite Serialisierungsschemas.
Im Playground öffnen// Wire types are schema-checked serialization contracts. Each field carries
// an explicit tag (@1, @2, ...) so the binary encoding stays compatible as
// the schema evolves. Optional fields may be added or omitted across versions.
#[wire]
type UserMessage {
name: string @1;
age: i32 @2;
nickname: Option<string> @3 optional;
}
fn main() {
println("Wire types define serialization schemas.");
println("Fields have explicit tags (@1, @2) for");
println("backwards-compatible binary encoding.");
}