A tour
jihoo is a systems language: programs compile through LLVM to machine code that runs as fast as C, with no garbage collector and no runtime. Memory is safe without lifetime annotations: every value has one owner, functions borrow their arguments, and the compiler checks that nothing changes what is borrowed.
struct Point { x: i64 y: i64}
impl Point { fn norm2(self) -> i64 { self.x * self.x + self.y * self.y } fn shift(inout self, d: i64) { self.x += d }}
fn main() { var p = Point { x: 3, y: 4 } p.shift(1) println(p.norm2()) // 32}Variables
Section titled “Variables”let names a value that does not change, var one that does. Types of
locals are inferred; signatures are written out.
let n = 10 // i64var total = 0for i in 0..n { total += i}Functions borrow
Section titled “Functions borrow”A parameter x: T is lent to the function for the call: it can read it,
not change or keep it. inout x: T lends it to be changed, and the caller
writes & to show that. sink x: T gives the value away.
fn sum(xs: [i64]) -> i64 { ... } // readsfn reset(inout v: Vec(i64)) { v.clear() } // changesfn consume(sink s: String) { ... } // takes
reset(&numbers)Nothing is copied unless the program says so: values that own memory
(String, Vec, Box, anything holding one) move, and .clone() copies
them. Numbers and structs made of numbers copy freely. See
Values and ownership and Borrowing.
Structs, tuples, arrays, and enums with payloads; match takes them apart
and must cover every case.
enum Expr { Num(i64) Add(Box(Expr), Box(Expr))}
fn eval(e: Expr) -> i64 { match e { Num(n) => n Add(a, b) => eval(a) + eval(b) }}Generics
Section titled “Generics”A generic parameter is a compile-time parameter, and each set of arguments compiles to its own code. Bounds are traits.
fn largest(comptime T: Ord, xs: [T]) -> T { ... }
let words = Map(String, i64).new()Errors
Section titled “Errors”Option and Result are ordinary enums, and ? returns early with the
failure. Bugs stop the program with a message: an index out of bounds, an
integer overflow (in debug builds), panic(...).
fn parse_pair(a: str, b: str) -> Option(i64) { Some(a.parse_int()? + b.parse_int()?)}The standard library
Section titled “The standard library”Vec, String, Map, Box, sorting, files and printing are written in
jihoo, on top of unsafe code, and the same library works without
libc (Profiles). See the Standard library.