Code Example Template
Deep-Dive System Documentation
Functions: Structural Dispatch
Functions are the core building blocks of execution in Rust. They are declared using the fn keyword.
Type Safety Features
- Explicit Signatures: Unlike variables where types are frequently inferred, function parameters and return types must be declared explicitly. This keeps code contracts clear and facilitates fast compiler type checking.
- Return Expression: Functions return the value of the final expression in their block. Omit the semicolon to treat a line as an expression.
// Standard function emitting keyboard stroke
fn send_stroke(key_code: u32) {
println!("Emitting virtual key event: {}", key_code);
}
Diverging Functions (!)
Functions that never return (e.g., executing infinite OS message loop processes) return the special "never" type !.
fn event_loop() -> ! {
loop {
poll_input_hooks();
}
}
Quick Reference Guide
fn add(x: i32, y: i32) -> i32 { x + y }