Unchecked Code Syntax
This section describes the syntax for unchecked code constructs.
Unchecked Functions
A function MAY be marked with the unchecked modifier to indicate that calling it requires a checked block.
The unchecked modifier requires the unchecked_code preview feature. Using unchecked without enabling this feature is a compile-time error.
function = [ "pub" ] [ "unchecked" ] "fn" IDENT "(" [ params ] ")" [ "->" type ] "{" block "}" ;
unchecked fn dangerous_operation() -> i32 {
42
}
pub unchecked fn public_dangerous() -> i32 {
0
}
Checked Blocks
A checked block is an expression that enables unchecked operations within its body.
checked_expr = "checked" "{" block "}" ;
A checked block evaluates its body and returns the value of the final expression. The type of a checked block is the type of its body expression.
fn main() -> i32 {
let x = checked {
let a = 10;
let b = 32;
a + b
};
x
}
Raw Pointer Types
Rue provides two raw pointer types for low-level memory access:
ptr const T- a pointer to immutable data of typeTptr mut T- a pointer to mutable data of typeT
ptr_type = "ptr" ( "const" | "mut" ) type ;
Raw pointer types are parsed in Phase 1 but semantic analysis (type checking, pointer operations) is implemented in later phases. Until Phase 2 is implemented, using pointer types results in an "unknown type" error.
// These parse correctly but fail type checking until Phase 2
fn takes_ptr(p: ptr const i32) -> i32 { 0 }
fn takes_mut_ptr(p: ptr mut i32) -> i32 { 0 }
unchecked fn get_ptr() -> ptr const i32 { @panic() }