Skip to content

Raw Pointers

Calibre supports raw pointer types with ptr:<T>.

A raw pointer is a low-level reference to memory. It is useful for systems-level interop and advanced runtime work, especially when interacting with foreign code.

Here, the target pointer type is ptr:<int>, meaning “a raw pointer to an int”.

let p_result := 42 as ptr:<int>;

Raw pointers are different from ordinary references like &T and &mut T.

  • &T and &mut T are ordinary language references used in normal Calibre code
  • ptr:<T> is a raw pointer type intended for lower-level operations and FFI

Because pointer conversions may fail they return result types unless as! is used.

let value := 42;
let p := value as ptr:<int>;
match p {
.Ok : val => print(try val as int => 0),
.Err : msg => print("err: " & msg)
};

You can also use the other cast modes:

let ptr_val := 42 as? ptr:<int>;
let raw_ptr := 42 as! ptr:<null>;

These behave like other Calibre cast forms:

  • as returns a result
  • as? returns an option
  • as! forces the cast and panics on failure

The special type ptr:<null> is useful when you want a very generic raw pointer without describing a more specific pointer type.

let any := 42 as ptr:<null>;

For ordinary application code, prefer normal values, references, options, and results whenever possible. Raw pointers are mainly for advanced and interoperable code.