News for hackers. Stuff that matters.
Re:Lisp-Stick on a Python by flohofwoe on Tuesday November 15, 2022 @02:59AM (#33606501)
> whereas in Rust you just return a `Vec` and everything is taken care of

The downside is that you just performed a hidden heap allocation. If automatic memory management is desired, a garbage-collected language might have been the better choice in the first place.

Re:Lisp-Stick on a Python by xigoi on Tuesday November 15, 2022 @06:00AM (#33607726)
How would you do it without a heap allocation?
Re:Lisp-Stick on a Python by flohofwoe on Tuesday November 15, 2022 @07:29AM (#33608309)
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:

    typedef struct {
        int items[4];
    } IntArray4;

    IntArray4 get_array(void) {
        return (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 in registers). That's one of the few cases where Rust code actually turns out more simple than the C equivalent ;)