So if you have a C program, and you want to dip your toes in Zig-land by writing the next library in Zig, you just can. If you chose Rust instead, you're going to have a bad time.
Naw.
/* main.c */
#include <stdio.h>
#include <stdint.h>
uint8_t answer();
int main(int argc, char** argv) {
printf("%d\n", answer());
return 0;
}
// example.rs
#[no_mangle] extern "C" fn answer() -> u8 {
println!("The answer is:");
42
}
# test.sh
rustc --crate-type staticlib -o example.a example.rs
gcc main.c example.a -lpthread -ldl
./a.out
$ ./test.sh
The answer is:
42
Workflows using `cargo` instead of `rustc` are also sane and easy. #[no_mangle] extern "C"
All due respect to Rust, which is a fantastic language, what this line says is "here is a C function written in Rust".You can of course do the same thing with C++ and with D, allowing all three to check the box for interoperability. And with Rust you get everything Rust offers: so you can write in the `extern "C"` dialect while getting memory-safety guarantees within the library, that's not nothing.
What Zig offers is the ability to write ordinary Zig and call that from C. That's part of what it's designed to do.
This is interoperability by design, rather than by configuration. It's simply a different thing, and considered along this singular dimension, I would assert that it's better.
I didn't, and wouldn't, say that Zig has the potential to replace Rust. I said that about C, and explained why.
You say that, but Zig appears to suffer from the exact same problem you point out in my Rust code:
export
https://ziglang.org/documentation/master/#Exporting-a-C-Libr...Perhaps I'm mistaken. How do I directly call Zig's `std.debug.print` from C? Or Timestamp.unixEpoch as defined here? https://ziglang.org/documentation/0.7.1/#Doc-comments
There's a bunch of rust crates containing macros, binaries, and build scripts that cut down on Rust's boilerplate as well. While there are some rough edges and room for further improvement, I am far happier with Rust's C interop than I am with the C interop of most other languages. Zig might have a touch more polish, but the bit I originally quoted made Rust sound like it was a good order of magnitude worse than Zig at C interop, which I just can't agree with.
One of the very first things I did when learning Rust was write a test library and drop it into an existing C++ codebase. I've also experimented heavily with cross compiling it to pretty much anything I can get my hands on. It was a good time, not a bad one.
However that compatibility is also what makes Objective-C and C++ impossible to ever be fully safe, without breaking compatibility with the C subset.
Static analysis help, but only when all code is available.
Nim, if you let it use C as a backend, will let you use C macros, despite being generally safe and not remotely a C superset.
Yes, the use of C macros (and any kind of ffi) is unsafe, that cannot be avoided. But it doesn’t need to be a c superset (and indeed, when you use JS as a backend, or the native LLVM backend, you get no access to C macros)
Obviously, you can’t expect people that didn’t managed to learn these languages to know that.
But if that’s your killer Zig feature, lots of languages have it.
You can use defer keyword to free right after allocating and it won't be called until the function/program completes. You can use debug or testing allocators to ensure no memory is left unfree'd or double free'd. The std lib has a large selection of custom allocators that you can use depending on your needs. One great one is an "Arena Allocator" which is just a linked-list of allocations that can be free'd at one time, versus tracking all these individual allocations. And if none of these allocators fit your needs, the interface to define one is pretty easy to implement. https://ziglang.org/documentation/0.7.1/#Choosing-an-Allocat...
"I'm not mutating or holding onto anything I'm not meant to"
Not really sure Zig can help you with this one. With great power comes great responsibility, or something like that.
"that I've accounted for every exception that I can result from a given call"
Zig does not have exceptions, but it does have error types which functions can return. From the function definition you usually can determine all the errors that can possibly be returned, unless the function definer use'd the catch all ! error type. Regardless, it is still much better than regular exceptions because
- you know by the function def that an error could be thrown, the compiler forces the caller to account for a possible error by "unwrapping" the return value
- an error happening just passes back a different type to the caller and in combination with the above, you can never "miss" an error https://ziglang.org/documentation/0.7.1/#Errors
Also Zig switch statements without an else ensure you handle every possible value so if you forget to handle one possible error (based on the type), it will throw a compile error. https://ziglang.org/documentation/0.7.1/#toc-Exhaustive-Swit...
There's also ZLS, a language server implementation for Zig that I believe can help with that (or if not today, it will in the future).
this can do that quite easily afaik.
Learning Rust kind of feels like brute-forcing my brain until it sticks. But I can't say it isn't fun and exciting!
I was planning on using Nim but might give Zig a try also.
It looks really cool. I feel like Nim and Zig both fill a similar niche, maybe Zig aiming more towards the lower level / even embedded.
That's just my impression, not having used either in anger.
I agree on the C++ puzzle solving thing, though in a way it's almost part of the appeal!
while the debug allocator can tell you at runtime about use-after-free or memory leaks, that's a less strong guarantee than rust or a GCed language
there are no closures; in general the code you write is lower-level than code in python, c#, rust
the language is very new
--opinions of a zig dilettante
bonus observations:
the std library code is very readable
the language overall has an excellent philosophy & design approach, and i predict it will become extremely popular
(as a single point of comparison: even after puzzling over it for a while, i still don't understand how modules work in rust; the equivalent feature in zig is immediately obvious, and in fact is the same concept as structs. how cool is that? (very cool, imo.))
I think that's very important feature to have for some people like me. I love Java standard library, because it's really easy to read. I regularly read its source code when I need to understand something and it's amazing how much time that could save. The only nitpick when it comes to Java is that some low-level stuff implemented in C and it's not as straightforward to jump into C implementation.
And that what turns me off from Scala and from C++. Scala collections library is horrible to read. It's immensely complicated. I get that they're solving hard problem, but I don't like it. And I hate C++ STL code (at least those that I saw), they're using obscure __identifiers__ and so on (may be there are better implementations, I saw GCC and MSVC ones).
Standard library is how language is supposed to be used. Those who learn language can just read its sources to better understand language idioms and replicate those in their code. And there's a huge difference between languages with readable standard library and languages with unreadable standard library (or even without sources at all, Apple sucks).
If only we could skip forward a decade and get mature implementations of it right now...
this is why the world is such a sad place
Also, currently it's possible to return a pointer/slice to a stack-allocated array from a function without a warning or error resulting in a dangling pointer. But this too will hopefully be fixed (because even C compilers warn about this these days).
But I think all those points can be considered bugs / todos.
It’s a great language though and a ton of fun. It’s so easy to dive into a codebase quickly which I value as a very distracted dad. The standard lib is a joy to read too.
¹ https://github.com/ziglang/zig/search?q=design+flaw&type=iss...
They have runtime checks (unlike Rust compile checks) which are at least slower, but likely still don't guarantee memory safety (did not dig deep).
even if the language is great, if all the ecosystem is written by people that didn’t manage to learn C++ in 25 years, I can’t really trust any library there
- The language is really simple and "enjoyable". Writing C++ often feels like puzzle solving to me even after 25 years. From the little Rust I've written so far, I got the same impression. Writing Zig is like writing C or Go but even more straight-forward. The language completely "disappears" while coding.
- The simple integration with C, Objective-C and C++ doesn't get enough credit (disclaimer: I haven't tried C++ yet). Even if Zig aims to replace C, I think the reality will be that most cross-platform / "cross-language" libraries will continue to be written in C, and most "serious" Zig projects will actually be mixed-language projects. Most other languages are too much trapped in their own ecosystem and aim to "rewrite the world" (which won't happen of course).
- Integrating the build system into the compiler and standard library is the right way to do it (not a feature unique to Zig, but if the C/C++ world is looking for the right way to solve the "build system problem", this is it).
...lots of other cool stuff in Zig to write about such as the incredibly simple module system, the comptime-, reflection- and generics-features, but IMHO the only feature that really counts in the end is: ergonomics.
This is the one thing that matters and where Zig beats the more "powerful" alternatives.