8.15 Variadic Functions and Macros

Rust does not support C-style variadic functions (using ...) directly, but you can call them from unsafe blocks if necessary (such as when interacting with C). For Rust-specific solutions, macros generally provide more robust alternatives.

8.15.1 C-Style Variadic Functions (for Reference)

#include <stdio.h>
#include <stdarg.h>

void print_numbers(int count, ...) {
    va_list args;
    va_start(args, count);
    for(int i = 0; i < count; i++) {
        int num = va_arg(args, int);
        printf("%d ", num);
    }
    va_end(args);
    printf("\n");
}

int main() {
    print_numbers(3, 10, 20, 30);
    return 0;
}

8.15.2 Rust Macros as an Alternative

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Macros can accept a variable number of arguments and expand at compile time, providing functionality similar to variadic functions without many of the associated risks.