News for hackers. Stuff that matters.
Re:Zig now has built-in HTTP server and client in std by bobbylarrybobby on Thursday May 18, 2023 @05:35PM (#35994398)
My understanding is that rust is a better C++, Zig is a better C. They're better than their respective antecedents in different ways (Rust is memory safe, Zig is ergonomic).
Re:Zig now has built-in HTTP server and client in std by slondr on Thursday May 18, 2023 @06:00PM (#35994717)
I see this line repeated a lot, especially in Zig spaces, but it doesn't really make sense to me. Rust is an excellent replacement for C, even though it has more features. What makes C the language of choice for so many applications is not its lack of features.
Re:Zig now has built-in HTTP server and client in std by drowsspa on Thursday May 18, 2023 @07:10PM (#35995576)
It's been a long time since I've programmed in C, but AFAIK the major pain points were and continue to be the build system and the preprocessor (or consequences of those). Zig does seem to fix in a pretty simple way
Re:Zig now has built-in HTTP server and client in std by mananaysiempre on Thursday May 18, 2023 @07:24PM (#35995713)
> I see [Rust : Zig :: C++ : C] repeated a lot, especially in Zig spaces, but it doesn't really make sense to me. Rust is an excellent replacement for C, even though it has more features.

It’s the feels, not the features :) Rust code feels like C++ code, at least when you’re reading it (I haven’t written any worth talking about). It puts the problem domain in similar terms, it has similar transparency (or lack thereof) regarding what it’s copying or allocating or whatnot, and so on. In that respect (!) they are closer to each other than C is to either—at least vanilla C, not a DSL (“language overhaul mod”?) like GObject. And it makes sense to target the C side of that divide in a new language (though again I lack the experience to say to which degree Zig succeeds in hitting that target).

Re:Zig now has built-in HTTP server and client in std by pornel on Thursday May 18, 2023 @08:33PM (#35996318)
The feels are not very accurate. Rust adopted C++-like syntax to look less weird to C++ programmers, but semantically it is quite a different language. It's more like a low-level Ocaml than C++.

Notably:

• Rust doesn't have inheritance. This makes a lot of basic C++ programming patterns unfit for Rust, and prevents 1:1 translation of C++ to Rust.

• Rust's generics may look like C++ templates, but they're not. Rust's macros behave more like C++ templates, and generics are closer to C++ concepts, but neither is a close match. C++ programmers are generally flabbergasted how hard is to make a function that takes any integer type in Rust.

• Even though Rust copied C++ moves, and has "RAII", the way these are used in practice ends up different due to having opposite defaults and different guarantees. Rust doesn't have constructors. Its closest equivalent of exceptions is for a different purpose. Rust's types are always movable, don't have meaningful addresses, can't reference own fields. Even C++'s std::string is hopelessly incompatible with Rust.

C++ has two string types, because of C legacy. Rust has two (and more) string types to express different modes of ownership. C++ is a complex language, because they keep adding more ways to initialize a variable. Rust has exactly one way. But Rust has many other features, mostly in its type system, to define thread-safety, memory-safety, and memory management in detail that is beyond what C++ can express. So "but they're both big" is glossing over all the reasons why.

However, C happens to be almost a clean subset of Rust. You can take a C program and translate it line by line to Rust. It won't be idiomatic, but may be easy to refactor into proper Rust. That's generally not true with C++, which requires rethinking everything from basic idioms and constructs to the overall architecture. This is why Rust struggles with GUI libraries, and the best-supported native toolkit is from C.

Re:Zig now has built-in HTTP server and client in std by zeroxfe on Thursday May 18, 2023 @10:52PM (#35997183)
> However, C happens to be almost a clean subset of Rust. You can take a C program and translate it line by line to Rust.

I can't see how that's possible, unless you litter your code with `unsafe` all over the place. Rust requires a lot of restructuring around ownership and borrowing to get your code to even compile.

Re:Zig now has built-in HTTP server and client in std by pornel on Thursday May 18, 2023 @11:22PM (#35997344)
I've done it with a few libraries. They usually have defined ownership, but it's specified in the library's manual, not in the code. Even unusual patterns can generally be mapped to Cow or Option or worst case some custom smart pointer with minimal amount of unsafe. But very often merely converting pointers to & vs &mut vs Box as appropriate gets the job done.

The most common sin is C libraries taking just a pointer and hoping for the best, instead of pointer+length for buffers (slices). But that's also a "local" problem you can fix by adding an extra argument, generally not a major redesign.

Thread-safety tends to be worse to map, because C reasons about safety of function calls, while Rust about sharing of data types. So "it's safe to call foo unless option bar is set to -5" doesn't translate well.

Re:Zig now has built-in HTTP server and client in std by pjmlp on Friday May 19, 2023 @02:57AM (#35998642)
Rust has traits inheritance.
Re:Zig now has built-in HTTP server and client in std by pornel on Friday May 19, 2023 @06:53AM (#36000064)
Sibling comment also says "has interface inheritance with pretty much the same syntax", but this is another case where people see the same ASCII char and think it's the same feature, when it's so different.

The A:B syntax is not inheritance, but a syntax sugar for 'A where Self:B' bound.

The result is these remain separate traits without support for subtyping. dyn trait A:B can't be used where B is required, unless you have access to the concrete type and make a new vtable for B from scratch. There's WIP to fix that, but not here yet. The auto traits that look like subtyping are for built-in marker traits only.

Lack of data inheritance is painful. There's no support for fields in traits. Getters/setters are problematic due to borrowing all of self instead of just the field.

Traits can't have private or protected methods.

Traits aren't inherent to the types, so you have to import both the type and the trait to use it. It's messy and makes interface docs confusingly fragmented.

So Rust really really isn't an OOP language, even though you can put together an awkward-to-use imitation from a few ill-fitting features — but they use the same sigils as OO in C++!

Re:Zig now has built-in HTTP server and client in std by pjmlp on Friday May 19, 2023 @07:18AM (#36000293)
Rust is OOP, regardless how many try to sell it as not, there are many ways to skin a cat.

You can enjoy my Rust version of Raytracing on one weekend, then again maybe not.

Just like being an FP language doesn't mean does it like Haskell. Many of which even predate Haskell's birthdate.

Plenty of ACM, SIGPLAN, IEEE, and EUROCOOP papers on the matter, including a few ones from a certain Simon Peyton Jones.

Re:Zig now has built-in HTTP server and client in std by alchemio on Friday May 19, 2023 @04:26AM (#35999255)
C++ is the most important source inspiring Rust throughout its development. Rust devs just like to downplay that role.

- Rust has interface inheritance with pretty much the same syntax

- Rust’s generics are monomorphized, exactly like C++ templates. They also have the same syntax. The only difference is that Rust generics are trait bound.

- Rust RAII is used exactly as it’s used in C++. Moves in Rust are destructive, while in C++ they are not. At the time where move semantics were proposed for C++ in the C++03 era, both types were proposed and the committee decided to go with non-destructive moves.

- Scope resolution syntax is also a carryover from C++.

When the words superset and subset are used in regards to C, C++ and Objective-C, it is quite a misunderstanding to describe C as a subset of Rust.

Re:Zig now has built-in HTTP server and client in std by pornel on Friday May 19, 2023 @07:05AM (#36000157)
Rust is inspired by C++ the same way JavaScript was inspired by Scheme.

Yeah, there are some borrowed concepts, but it's a different language.

Re:Zig now has built-in HTTP server and client in std by Mawr on Thursday May 18, 2023 @11:37PM (#35997448)
That is true in the pragmatic sense, those who only use C for its objective qualities would find Rust a good replacement.

However, those who use C because they like the language would not enjoy Rust one bit and would much prefer Zig over it.

Re:Zig now has built-in HTTP server and client in std by hansvm on Friday May 19, 2023 @02:43AM (#35998563)
Rust limits programs (even when you use unsafe) to those you can prove correct to the copmiler, and it has a habit of taking on more dependencies than necessary for a given task (using syscalls and locks when not required, allocating frequently, ...). It's a nice language, but it made tradeoffs, and a meaningful fraction of C code would be hard to port to or even link from Rust.
Re:Zig now has built-in HTTP server and client in std by brigadier132 on Thursday May 18, 2023 @06:04PM (#35994771)
This is a silly way of comparing these languages.

Zig and rust are both modern iterations of low level languages.

Rust chooses safety with more complex syntax. Zig chooses a less complex syntax with less safety.

Re:Zig now has built-in HTTP server and client in std by jadodev on Thursday May 18, 2023 @07:02PM (#35995496)
nah this is a pretty normal way to compare languages. rust's "complex" syntax can mostly be attributed to its semantics https://matklad.github.io/2023/01/26/rusts-ugly-syntax.html safety can also mean a lot of different things: https://andrewkelley.me/post/unsafe-zig-safer-than-unsafe-ru...
Re:Zig now has built-in HTTP server and client in std by brigadier132 on Thursday May 18, 2023 @08:02PM (#35996066)
How is it a good way to compare the languages when Rust is nothing like C++?
Re:Zig now has built-in HTTP server and client in std by jadodev on Thursday May 18, 2023 @08:16PM (#35996168)
... wat? imperative; compiled to machine code, either static exe or dynamic libs; builtin RAII & polymorphism (parametric, subtyping); maps, vectors, trees, and other data structures in the stdlib; raw access to pointers; C ABI compat. there are some big differences, but "nothing like c++" is absolutely not true.
Re:Zig now has built-in HTTP server and client in std by FpUser on Thursday May 18, 2023 @06:17PM (#35994934)
It is not "better" it is different. Rust is "safer" although with the modern C++ along with tooling I do not really encounter any safety issues for about 3 years already. Looking at feature set C++ has way more. In my opinion Rust sits somewhere between C and C++.
Re:Zig now has built-in HTTP server and client in std by pjmlp on Friday May 19, 2023 @02:53AM (#35998623)
Zig is Modula-2 with C like syntax, plenty of @, no support for binary libraries.

What it has over Modula-2 feature checklist is mostly comptime.