Comment by deevus
2 months ago
I've somehow avoided Rust, so I can only comment on what I see in the documentation.
In Zig, you can just import a C header. And as long as you have configured the source location in your `build.zig` file, off you go. Zig automatically generates bindings for you. Import the header and start coding.
This is all thanks to Zig's `translate-c` utility that is used under the hood.
Rust by contrast has a lot more steps required, including hand writing the function bindings.
You only hand-write function bindings in simple or well-constrained cases.
In general, the expectation is that you will use bindgen [0].
It's a very easy process:
1. Create a `build.rs` file in your Rust project, which defines pre-build actions. Use it to call bindgen on whatever headers you want to import, and optionally to define library linkage. This file is very simple and mainly boilerplate. [1]
2. Import your bindgen-generated Rust module... just use it. [2]
You can also skip step 1: bindgen is also a CLI tool, so if your C target is stable, you can just run bindgen once to generate the Rust interface module and move that right into your crate.
[0]: https://rust-lang.github.io/rust-bindgen/
[1]: https://rust-lang.github.io/rust-bindgen/tutorial-3.html
[2]: https://github.com/Charles-Schleich/Rust-Bindgen-Example/blo...
Zig is easier than Rust here, but you can auto generate bindings, you don’t have to write them by hand.