Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Since the OP wanted to return an array, the correct answer would probably be to return a simple native array, not a complex stdlib container:

    pub fn get_array() -> [i32;4] {
        return [1, 2, 3, 4];
    }
In C one needs to wrap the array in a struct type to work around the pointer decay:

    struct IntArray4 { int items[4]; } get_array(void) {
        return (struct IntArray4) { .items = { 1, 2, 3, 4 } };
    }
Both solution don't use heap allocation but pass around the array data by value on the stack or packed into register and should compile to the same assembly code (ABI differences aside).

That's also one of the rare cases where Rust code actually turns out more simple and readable than the C equivalent ;)



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: