Generic Functions
Type parameters are declared in angle brackets after the function name and stand for a concrete type supplied at the call site. One generic definition replaces a family of identical overloads:
// Regular function
func Add(x: int, y: int) -> int {
return x + y;
}
// Generic function
func Min<T>(x: T, y: T) -> T {
return x < y ? x : y;
}
The type parameter T is bound to a concrete type for each call. It is usually inferred from the arguments, so the call site looks like any other:
let a = Min(3, 7); // T = int
let b = Min(2.5, 1.0); // T = float64
A single generic definition therefore replaces the family of near-identical overloads you would otherwise write by hand.
See Also
- Function Declaration — the non-generic form
- Type Aliases — naming the types a generic is instantiated with
Variadic
A variadic parameter accepts any number of arguments of a given type. It must be the last parameter and is declared by appending ... to the type. Inside the function the parameter is a slice of that type.
Assembler
An asm func replaces the usual Rux body with a sequence of x86-64 instructions written by hand in Intel syntax (the destination operand comes first). Reach for one only when you need exact control over the emitted machine code — a system-call stub, a CPU-feature probe, or a routine the language cannot otherwise express — and are prepared to manage registers, the stack, and the calling convention yourself. The body is assembled straight to machine code, bypassing the normal compilation pipeline.