Result

1

From Return Codes to Result

5 minute

C developers know this pattern well:

int read_config(const char *path, struct Config *out);

The function returns 0 on success and a non-zero value on failure; the real result is written through an output parameter. System calls often use -1 plus errno. With more resources, the code usually grows a goto cleanup path.

This works, but it has long-term costs:

  • callers may forget to check the return value
  • success values and error codes live in different places
  • output parameters need rules for failure cases
  • integer error codes often lose context
  • cleanup paths get tangled with error propagation

Rust Result is not an exception mechanism. It is closer to putting “success or failure” directly into the function return type.

Read More