C for Rust Programmers

(bd103.dev)

11 points | by torutofu 2 days ago ago

4 comments

  • hgs3 11 hours ago

    > I really love Rust's error handling. Result forces you to address errors ...

    > C's error handling story, in comparison, is straight up tragic. It seems to boil down to functions returning a "magic integer" like -1 or a null pointer to signal there was an error.

    In C23 you can add the [[nodiscard]] attribute to the declaration of an enum or struct type, then at every call site of every function that returns said type, the C compiler will emit a warning if the result is unused. Instruct the compiler to treat warnings as errors and you can't ignore them.

    Interestingly, Zig's error handling is conceptually C like, but limited to integer-like enums (no structs). It's basically a union of error integer code and success result. However, despite being limited to integers, Zig has dedicated compiler machinery for this so there's idiomatic consistency in Zig error handling.

  • xyzzy_apps a day ago

    I've tried to build a C like rust transpiler - https://github.com/xyzzyapps/carbide

    I hope it can be useful for writing C ABI bindings especially for things like CLAP audio plugins. Writing C ABI plugins in Rust is needlessly painful. Crystal has a much easier way to write C bindings.

  • mizmar 2 days ago

    That linked stackoverflow post about dereference operators is really interesting. I guess it answers my question why do most struct members in posix have a prefix abbreviating name of the containing struct.

  • black_13 21 hours ago

    [dead]