unsafe

Raw pointers, C functions and system calls are allowed in unsafe code. The standard library builds Vec, String and Map this way; programs that do not say unsafe cannot corrupt memory.

jihoo run examples/unsafe.jh
import std.libc

struct Pair {
    a: i32
    b: i32
}

fn main() {
    var p = Pair { a: 1, b: 2 }
    let ptr = &p.b          // a raw pointer, `*i32`
    unsafe {
        *ptr = 40
        libc.printf("%d\n", p.a + p.b + 1)
    }
}

View on GitHub

Esc