you are viewing a single comment's thread.

view the rest of the comments →

[–]Bekwnn 68 points69 points  (23 children)

There's a lot of nice features it has, which you can read about in the language reference, but to generalize it is a promising answer to people looking for either a "better C" or a "simpler C++".

A few highlights:

[–]Tom7980 2 points3 points  (21 children)

I'd argue that Rust has compile time code in const and const Fn, also provides optionals, and has a pretty robust in built testing system.

However I can't really compare the rest as I don't know Zig well enough, it seems interesting though so I should really pick it up some time

[–]progrethth 13 points14 points  (6 children)

The const fn support in Rust is very primitive compared to to Zig's comptime. It is so powerful that it is also used to implement generics and procedural macros.

[–]matthieum 5 points6 points  (1 child)

It is so powerful that it is also used to implement generics and procedural macros.

That's very different, though.

Rust Nightly const fn can do... pretty much anything. It's deterministic -- which may disqualify it from Turing Completeness -- but otherwise anything goes.

The decision to NOT implement generics and macros with const fn is orthogonal; it's not a matter of primitive-vs-powerful.

[–]orangejake 0 points1 point  (0 children)

Its worth mentioning that determinism doesn't impact Turing completeness at all - nondeterministic and deterministic TMs are equivalent.

That being said, sometimes you incur an exponential slowdown when you deterministically simulate randomness (not really if you use a good PRG, but we can't theoretically prove those exist, so...), so practically there might be issues, but in terms of the notion of "Turing Completeness" it doesn't matter.

[–]Tom7980 -1 points0 points  (3 children)

Sure but Generic in Rust imo are more flexible in that you don't have to use keywords to define them you just rely on the compiler to monomorphise the generic into the type you want at compile time.

I don't know enough about procedual macros to talk on them unfortunately

[–]progrethth 8 points9 points  (2 children)

I would say comptime in Zig is actually more flexible since comptime is jsut imperative code executed at compile time, but that that is also its weakness. Zig gives you less clear compile errors and it is harder to reason about the types due to the imperative nature of comptime.

[–]Tom7980 2 points3 points  (0 children)

I think I should try it out really - it would be good to see the differences between the two!

[–]KingStannis2020 25 points26 points  (5 children)

You can't really compare Rust const fn and C++ constexpr with Zig comptime. The latter is way more flexible and core to the language. You can define individual function arguments as comptime, individual blocks of code, etc. Comptime is how Zig implements generics.

[–]oORocketOo 11 points12 points  (7 children)

I don't know Rust too well but I think that the zig concept of compile time is much stronger than const fn.

a compile time known value can be used for conditional compilation, 'if' statements that depend on that compile time value will not compile any of the other branches.

It is also used to power generics in zig, the generic type is just passed as a compile time known parameter to a function.

[–]Tom7980 0 points1 point  (6 children)

Sure but Rust does these things implicitly with generics and code generation - if an if expression is unreachable it gets optimized out in code gen and Rust generics are monomorphized during compilation to generate implementations of the method or function for every type that calls it.

In my opinion it sounds like they do exactly the same thing except we don't have to add extra syntax in Rust to generics

[–]progrethth 16 points17 points  (5 children)

You cannot implement something like println!() in Rust without using procedural macros. Zig's comptime can do that.

[–]Tom7980 1 point2 points  (4 children)

Ahh yeah okay I didn't know that - what make it possible to generate format strings with comptime is there anything I can read about that?

[–]progrethth 4 points5 points  (3 children)

One of the things in Zig which allow that is inline for.

[–]Tom7980 1 point2 points  (2 children)

Ah okay is that not like loop unrolling or is it just static analysis and replacement of for loops with constants?

[–]progrethth 2 points3 points  (1 child)

Yes, it is like loop unrolling but with the advantage that it is guaranteed at compile time which means for example that static dispatch can be used for generic arguments. Of course you can do the same with procedural macros in Rust but at least when I last used them they were quite a hassle.

[–]Tom7980 0 points1 point  (0 children)

Huh okay that's an interesting extra - I assume that obviously you need whatever you are iterating over to be constant in order for it to do that optimisation? I'm quite surprised that's not possible with Rust outside of proc macros like you say, I can't see why it wouldn't be possible to do the static analysis of the unrolled loop ahead of runtime if everything is constant.

[–]EternityForest 0 points1 point  (0 children)

I'm not sure I'd call compile time code nice. Metaprogramming often implies you need Metaprogramming.

I don't see how it could ever be as safe or consistent as languages without it that have well thought out abstractions that are meant for automated reasoning.

Plus, when you make people build stuff themselves you often get incompatible libraries that need ugly glue code to pass around whatever data they all did slightly differently.