2

In C it can be better to pass pointers to larger structs.

Example of return-by-value:

typedef struct LargeStruct {
    char data[512];
} LargeStruct;

/* snip */

void some_function() {
    for (int i = 0; i < total; ++i) {
        LargeStruct value = some_other_function();
        /* use 'value' */
    }
}

In this case its often preferable to pass a pointer to the struct. eg:

    for (int i = 0; i < total; ++i) {
        LargeStruct value;
        some_other_function(&value);
        /* use 'value' */
    }

... so each function call doesn't have to make a copy of the struct.

My question is: Does this apply to Rust too?

Are return values handled differently to C that make it unnecessary to pass return-arguments?

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • 3
    Also relevant: https://stackoverflow.com/questions/27835375/can-i-efficiently-return-object-by-value-in-rust – viraptor Aug 05 '16 at 07:05

0 Answers0