Intrinsic Expressions
An intrinsic expression invokes a compiler-provided primitive operation.
intrinsic = "@" IDENT "(" [ intrinsic_arg { "," intrinsic_arg } ] ")" ;
intrinsic_arg = expression | type ;
Intrinsics may accept expressions, types, or a combination of both as arguments, depending on the specific intrinsic.
Intrinsics are prefixed with @ to distinguish them from user-defined functions.
Each intrinsic has a fixed signature specifying the number and types of arguments it accepts.
Using an unknown intrinsic name is a compile-time error.
@dbg
The @dbg intrinsic prints a value to standard output for debugging purposes.
@dbg accepts exactly one argument of integer, boolean, or string type.
@dbg prints the value followed by a newline character.
The return type of @dbg is ().
fn main() -> i32 {
@dbg(42); // prints: 42
@dbg(-17); // prints: -17
@dbg(true); // prints: true
@dbg(false); // prints: false
@dbg(10 + 5); // prints: 15
@dbg("hello"); // prints: hello
0
}
@dbg is useful for inspecting values during development:
fn factorial(n: i32) -> i32 {
@dbg(n); // trace each call
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
fn main() -> i32 {
factorial(5)
}
@size_of
The @size_of intrinsic returns the size of a type in bytes.
@size_of accepts exactly one argument, which must be a type.
The return type of @size_of is i32.
The value returned by @size_of is determined at compile time.
fn main() -> i32 {
@size_of(i32) // 8 (one 8-byte slot)
}
struct Point { x: i32, y: i32 }
fn main() -> i32 {
@size_of(Point) // 16 (two 8-byte slots)
}
@align_of
The @align_of intrinsic returns the alignment of a type in bytes.
@align_of accepts exactly one argument, which must be a type.
The return type of @align_of is i32.
The value returned by @align_of is determined at compile time.
All types in Rue currently have 8-byte alignment.
fn main() -> i32 {
@align_of(i32) // 8
}