News for hackers. Stuff that matters.
Re:Rust vs. C++ Formatting by mr_00ff00 on Sunday January 15, 2023 @03:25PM (#34392946)
Finally, after 40 years technology has advanced far enough for C++ to have a print function instead of cout
Re:Rust vs. C++ Formatting by asguy on Monday January 16, 2023 @06:14AM (#34399476)
Re:Rust vs. C++ Formatting by Jensson on Monday January 16, 2023 @03:34PM (#34405157)
Which is a C function and not safe at all. It is easy to make your own print function in C++ that is safe and easy to use, but there is no such function in std.
Re:Rust vs. C++ Formatting by adamdusty on Monday January 16, 2023 @03:44PM (#34405247)
Why are you worried about how safe printing to terminal is? Genuinely curious, I don't work in software dev.
Re:Rust vs. C++ Formatting by yakubin on Monday January 16, 2023 @03:59PM (#34405401)
In C you can use “%g” printf format string (which indicates a value of type double), and then not pass a double to it, but e.g. an int. Easy mistake to make, when changing pre-existing code.

On x86 what will happen is the code will compile, but the function is going to read its argument from a floating point register instead of an integer register as it should. This:

1. Is a bug, since a completely unrelated garbage value is going to be printed.

2. Leaks the value of a register, which may be a security issue.

There are still other common issues which can easily turn into vulnerabilities, leaking private process memory, when people pass untrusted strings as format strings with the intention of printing them raw.

So you want a safe print to prevent trivial bugs in general, and security vulnerabilities in particular.

Re:Rust vs. C++ Formatting by __david__ on Monday January 16, 2023 @08:47PM (#34408182)
You’re not wrong, but on the other hand, every C compiler I’ve used for the past 25 years has at least a warning you can enable for that. And you can easily add some attribute to custom functions that make use of *printf() functions under the hood to get those also type checked by the compiler. In practice that’s been good enough for me (and catches exactly the type of error you describe).
Re:Rust vs. C++ Formatting by charcircuit on Tuesday January 17, 2023 @01:45AM (#34409767)
Usually the warning is enabled by default
Re:Rust vs. C++ Formatting by jasode on Monday January 16, 2023 @03:52PM (#34405325)
>Why are you worried about how safe printing to terminal is?

The "type-safe" means "type-checked" by the compiler for correctness to help prevent bugs. It doesn't mean "safety-as-in-not-dangerous".

Re:Rust vs. C++ Formatting by maccard on Monday January 16, 2023 @04:05PM (#34405459)
You're not always printing to a terminal,

    char buf[10];
    const char* foo = "wrong?";
    int res = snprintf(buf, 20, "What could possibly go %d", foo);
Will compile and do... something...
Re:Rust vs. C++ Formatting by jholine on Tuesday January 17, 2023 @04:54AM (#34410797)
Solved problem since ages ago.

error: format '%d' expects argument of type 'int', but argument 4 has type 'const char*' [-Werror=format=]

You only get into trouble when you use runtime format strings (like passing a user string as first argument to printf)

Re:Rust vs. C++ Formatting by maccard on Tuesday January 17, 2023 @10:21AM (#34413461)
That doesn't work on MSVC, and ignores multiple other issues with the 3 lines of code I shared (and as you correctly identified doesn't work with runtime format strings.

It also doesn't work if your code isn't a textbook example of being wrong. [0] is _slightly_ more contrived but still suffers all of the exact same problems, despite all of the information being available at compile time.

[0] https://gcc.godbolt.org/z/9vK3W4bYh

Re:Rust vs. C++ Formatting by gary_0 on Monday January 16, 2023 @04:21PM (#34405646)
printf() was often used for logging in eg. web servers. If there's no way of strictly checking the size/type of what's being printed (HTTP headers, say) then there are lots of tricky ways you can use it to write arbitrary memory and pwn the server.

Type-unsafeness in general also just allows for hard-to-find bugs, since only certain data at runtime will introduce undefined behavior.

Re:Rust vs. C++ Formatting by stevenhuang on Monday January 16, 2023 @05:29PM (#34406322)
There is now in C++20 https://en.cppreference.com/w/cpp/utility/format/format

Which is just the great https://fmt.dev/latest/index.html that even c++11 projects can use.

Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @03:35PM (#34405164)
It had C's printf, which means it isn't type safe, which is consequently a terrible primitive for this work. Like, it makes sense in C, which thinks boolean is a fancy new concept and thinks 0-terminated strings are a good idea, but it's not actually good.

std::println is more or less what you would obviously build for a modern language and it's notable because C++ could have provided something pretty similar even in C++ 98, and something eerily similar in C++ 11 but it chose not to.

Re:Rust vs. C++ Formatting by hn_go_brrrrr on Monday January 16, 2023 @03:49PM (#34405299)
I think you'll find implementing a type-safe print function in C++11 very challenging. What you could do in constexpr was very limited, type deduction was less powerful, and it didn't have fold expressions. I'd say this really only became feasible since C++17.
Re:Rust vs. C++ Formatting by Kranar on Monday January 16, 2023 @04:01PM (#34405418)
Boost had a type-safe printf function dating back to October 10th, 2002:

https://www.boost.org/doc/libs/1_31_0/libs/format/

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:27PM (#34405707)
And outstreams where not that bad, aswell. Sure, the operator overloading looks a bit rough. But that's IMHO a pragmatic choice if you want to offer customisation points and didn't have variadic functions yet. They where introduced only in c++11.
Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @04:34PM (#34405785)
C++ did have variadic functions because it inherited them from C.

What it didn't inherit from C was a way to write variadic functions with variadic types, so that had to be home grown.

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:50PM (#34405941)
Do you mean this feature [0]? I'm not aware of any differences in c and c++ about this. Can you get a type of a argument in C? How? At compile time, or at runtime? Both sound very un-C-like to me. cppreference is usually excellent documentation but it doesn't mention something like this.

I don't considers this to be "proper" variadic arguments, because a functions argument has to have a type. and these, as far as I'm aware of don't have one. This is about a powerfull as passing a void**. This is essentially memcopying multiple differently typed into a char* buffer and then passing that buffer. You can than correctly copies them back you have pretty much the same behaviour. Both methodss obviously lacks important aspects of the language abstraction of a function parameter and i don't what that feature can bring to the table that the previous techniques don't.

[0] https://en.cppreference.com/w/cpp/utility/variadic

Re:Rust vs. C++ Formatting by kevin_thibedeau on Monday January 16, 2023 @05:08PM (#34406098)
> Can you get a type of a argument in C?

You can get it indirectly using _Generic().

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @06:06PM (#34406742)
Sorry my bad. Yes, it works for proper function arguments. Does this work for variadic arguments? Parent seems to suggest, but i'm not aware of any mechanism for this.
Re:Rust vs. C++ Formatting by saghm on Monday January 16, 2023 @03:52PM (#34405333)
Maybe the top comment in this thread wasn't so facetious after all then; it did take almost 40 years for C++ to advance enough to have a decent print function
Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @04:09PM (#34405504)
Sure, a hypothetical C++ 11 equivalent would e.g. probably not be able to do the trick where we reject bogus formats at compile time, which is something programmers want and which Rust only gets to do by making this a fancy macro whereas C++ 23 is doing it in the actual library code.
Re:Rust vs. C++ Formatting by otherjason on Tuesday January 17, 2023 @07:51AM (#34411829)
fmtlib (https://github.com/fmtlib/fmt), a C++11 formatting library that was the basis for C++20 std::format, is able to do this.
Re:Rust vs. C++ Formatting by tialaramex on Wednesday January 18, 2023 @04:21AM (#34424884)
fmtlib does not, in fact, do this on C++ 11 because as explained it doesn't have enough constant evaluation.

The fmtlib code uses a C-style macro to try to handle format errors at compile time, which detects whether it has enough consteval and no-ops if it does not. On a modern C++ with enough consteval it does exactly what you'd expect it to do, but on older compilers it does nothing.

The result is that fmtlib on an older compiler gets you Exceptions, at runtime, for a format that was invalid at compile time, and if you upgrade the compiler it magically switches to compile time errors.

Edited to add quote from fmtlib docs:

"Compile-time checks are enabled by default on compilers that support C++20 consteval. On older compilers you can use the FMT_STRING macro defined in fmt/format.h instead. It requires C++14 and is a no-op in C++11."

Re:Rust vs. C++ Formatting by bee_rider on Monday January 16, 2023 @07:13PM (#34407392)
Booleans are weird though. A single bit on a system that mostly talks about bytes and bigger? How are you supposed to represent that?
Re:Rust vs. C++ Formatting by jchw on Monday January 16, 2023 @09:40PM (#34408541)
Basically like an enum? It's not that weird. It does not need to use a storage of one bit, just like C chars can technically be larger than eight bits. I suspect a bigger reason that boolean is missing from C is simply because C can't offer much additional value for boolean in its type system.

C++ can obviously do more with boolean types because it's got a more advanced type system, like disallowing values other than true and false to be assigned in code that only uses defined behavior. Boolean itself can take up whatever size the compiler decides it does, same idea as C. But, template types can special case boolean to use bitfields and allow for more space-efficient operations.

Re:Rust vs. C++ Formatting by LegionMammal978 on Monday January 16, 2023 @11:40PM (#34409216)
In the context of Rust, a bool is a 1-byte value with 2 valid representations (0x00 and 0x01) and 254 invalid representations (disregarding uninit bytes). The compiler can make use of these invalid representations for niche optimizations: For instance, an Option<bool> (i.e., std::optional<bool>) takes only 1 byte instead of 2, since the compiler can repurpose the invalid byte 0x03 to represent the None (i.e., std::nullopt) state of the Option. The compiler generally tries to perform these niche optimizations for all enums, although it's only 100% guaranteed to happen in certain cases.
Re:Rust vs. C++ Formatting by gmadsen on Monday January 16, 2023 @08:02PM (#34407833)
i believe this is in cpp23
Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:13PM (#34405550)
printf is a bad joke of a formatting function.

When i want to print a string i don't want to worry about the security implications of that. With printf i have to. [0]

And i certainly don't want a turing complete contraption. [1] Also looking at log4j.

And even if everything is correct, it's has to parse a string at runtime. I consider that alone unaesthetic.

>Edit: It's almost like the whole world got a lot of work done with the tools they already had.

The best metaphor i know for this attitude is "stacking chairs to reach to moon". If you don't care about the limits of the tech you will be stuck within it.

I'm time and time again amused how anti intellectual and outright hostile to technological progress the programming profession is. programmers, out of all of them.

[0] https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=printf

[1] https://news.ycombinator.com/item?id=25691598

Re:Rust vs. C++ Formatting by asguy on Monday January 16, 2023 @04:28PM (#34405723)
> If you don't care about the limits of the tech you won't be able exceed what you think is possible.

Did you propose/implement/release something better than printf?

> I'm time and time again amused how anti intellectual and outright hostile to technological progress the programming profession is. programmers, out of all of them.

Perfect is the enemy of good. Some people talk about getting work done, some people get the actual work done and move on.

Re:Rust vs. C++ Formatting by spoiler on Monday January 16, 2023 @04:46PM (#34405889)
> Perfect is the enemy of good. Some people talk about getting work done, some people get the actual work done and move on.

In my experience, people with this motto generally produce code which frustrates the whole team.

Being a perfectionist is toxic in its own way, though.

There needs to be a balance. I think that balance is to think and plan a few steps ahead (not too much, as it's counter productive) before hitting the keyboard. I know this sounds a bit like a "d'oh, of course" but it really—and unfortunately—isn't something that people practice; they just think they do.

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:52PM (#34405955)
>Did you propose/implement/release something better than printf?

This is what the article is about? Things much better that printf are a dime a dozed and available since 20 years.

>Some people talk about getting work done,

Like this article does? While you busy arguing that you could do the same thing, but much worse?

Re:Rust vs. C++ Formatting by Gibbon1 on Monday January 16, 2023 @05:25PM (#34406282)
Lets consider #embed which is new for C23. It allows you to import binary blobs of data into a C program at compile time. Like say if you want to import am image or sound file or a table.

How hard was that to implement? Seriously no reason it couldn't have been part of C89. Why wasn't it? Because the compiler writers and the C++ standards commit have no personal use for it. It took 40 years of waiting a five years to get it past the standards committee and just barely.

Those guys also have no interest in printf type functions. And improving printf would be a lot more work than implementing #embed.

Re:Rust vs. C++ Formatting by CountSessine on Monday January 16, 2023 @05:49PM (#34406560)
That’s neat - Borland C had the same thing with the `emit()` pseudo-function with their C89 compiler. I guess Borland’s compiler writers wanted it more than gcc’s?
Re:Rust vs. C++ Formatting by Gibbon1 on Monday January 16, 2023 @06:08PM (#34406767)
Consider the difference between what a compiler does and say a video game or embedded firmware. Compilers are old school batch mode programs that import data from a file, parse it, transform it to something, and emit it as a file.
Re:Rust vs. C++ Formatting by panzi on Tuesday January 17, 2023 @12:37AM (#34409484)
You can use ld to turn any file into an object file:

    ld -r -b binary -o foo_txt.o foo.txt
foo_txt.o then has these symbols:

extern const char _binary_foo_txt_start[]; extern const char _binary_foo_txt_end[]; extern const void *_binary_foo_txt_size;

So you need to write your own declarations (it doesn't generate a header file).

_binary_foo_txt_size is weird and has to be used as: (size_t)&_binary_foo_txt_size

Or use (size_t)(_binary_foo_txt_end - _binary_foo_txt_start) instead.

Re:Rust vs. C++ Formatting by chlorion on Monday January 16, 2023 @09:14PM (#34408358)
>some people get the actual work done and move on

These people's "actual work" often ends up causing endless streams of security vulnerabilities and bugs too.

Most of the same people you are referring to don't seem to believe that security vulnerabilities exist or are important enough to care about for some reason, but in the real world these are very important issues.

Re:Rust vs. C++ Formatting by scoutt on Tuesday January 17, 2023 @05:33AM (#34410991)
> These people's "actual work" often ends up causing endless streams of security vulnerabilities and bugs too.

On the other hand, we have people that apparently wouldn't make a program if they are not guaranteed (by another human being) that it will be safe.

If those people generating bugs and vulnerabilities would had to sit tight waiting for someone to make a safe language to do anything, today the world would be 40 years or more behind.

(safe languages that, sarcastically, were created using all those unsafe tools and insfrastructure)

Also in this real world a trillion of printf are being output right now, and will be for a long long time. Is the world falling apart?

You can also list all the printf CVEs but... how many println! are being output?

Re:Rust vs. C++ Formatting by usefulcat on Monday January 16, 2023 @10:41PM (#34408869)
> Perfect is the enemy of good.

Sure, but we're talking about printf here. printf is manifestly mediocre.

I guess 'perfect is the enemy of mediocre' doesn't have quite the same ring.

Re:Rust vs. C++ Formatting by Someone on Monday January 16, 2023 @05:48PM (#34406546)
> And even if everything is correct, it's has to parse a string at runtime. I consider that alone unaesthetic.

Technically, it doesn’t have to do that. If a program includes the header declaring printf using the <> header defined in the standard and then calls printf the compiler is allowed to assume that the printf that the program will be linked to will behave according to the standard, and need not compile a call to printf. It can generate code that behaves identically.

A simple example is gcc converting a printf with a constant string to a puts call (https://stackoverflow.com/questions/25816659/can-printf-get-...)

Re:Rust vs. C++ Formatting by syrrim on Tuesday January 17, 2023 @08:12AM (#34411965)
Everything has security implications in c, but printf isn't particularly bad. Common use of it involves a fixed format string specified at the call site. This prevents the most dangerous use of it (user specified format string) and also allows the compiler to detect when the format string doesn't correspond to the types of the arguments. Both these failures can be converted into compile time errors in common compilers. Printf, for all C's other faults, really isn't that bad.
Re:Rust vs. C++ Formatting by arcticbull on Monday January 16, 2023 @04:37PM (#34405822)
> Edit: It's almost like the whole world got a lot of work done with the tools they already had.

This feels a little defensive, but also pretty out of line with the philosophy of the C++ standards committee. The committee has been aggressively stapling every new leg they could find to that dog for decades. They just chose not to staple this particular leg on until now.

Re:Rust vs. C++ Formatting by simplotek on Monday January 16, 2023 @07:47PM (#34407718)
> This feels a little defensive, but also pretty out of line with the philosophy of the C++ standards committee. The committee has been aggressively stapling every new leg they could find to that dog for decades. They just chose not to staple this particular leg on until now.

Your comment doesn't bear any resemblance with reality. C++ started with a spartan standard library and only recently did it standardized it's file system API.

Compare that with what, say, POCO already offers. Or Boost. Or java/C#/Python/etc.

What exactly led you to believe that absurdity?

Re:Rust vs. C++ Formatting by arcticbull on Monday January 16, 2023 @08:04PM (#34407856)
> What exactly led you to believe that absurdity?

The fact that the standards committee simply chose to just add every feature every other language has.

Re:Rust vs. C++ Formatting by simplotek on Tuesday January 17, 2023 @03:32AM (#34410356)
> The fact that the standards committee simply chose to just add every feature every other language has.

Again, this take is outright wrong and totally clueless. I mean, the summary of each change introduced by any of the C++ standards is freely available. C++20's most compelling features beyond concepts and modules were small improvements over existing features like lambda captures and template resolutions, or new atributes.

What compells you to make such nonsensical claims?

Re:Rust vs. C++ Formatting by arcticbull on Tuesday January 17, 2023 @03:50AM (#34410435)
> What compells you to make such nonsensical claims?

Literally all the additions between C++03 and C++20.

Here's a small list since you'd rather attack me than review the changelogs, it seems.

- range-based for loops.

- enum class.

- digit separators and binary literals.

- consteval/constexpr/constinit.

- std::move

- std::forward

- std::variant based mock pattern matching.

- lambdas.

- structured binding declarations.

- an ABI for garbage collection.

- coroutines.

- concepts.

C++20 even added a three-way comparison operator.

This is just a random selection.

Re:Rust vs. C++ Formatting by spoiler on Monday January 16, 2023 @04:38PM (#34405829)
You could build anything you desire with only a hammer if you're creative enough
Re:Rust vs. C++ Formatting by dureuill on Monday January 16, 2023 @06:27PM (#34406984)
Yes, security researchers got quite a lot of work exploiting the antiquated tools of C coders

[1]: https://www.opencve.io/cve?cwe=CWE-134

Re:Rust vs. C++ Formatting by scrubs on Monday January 16, 2023 @11:37AM (#34402369)
Yah we'll get with the program. Printf in it's 86 varieties has been around for years.

Second, I'm increasingly associating that brit 70s show "keeping up appearances" with rust specifically that overly blushed face of the woman protagonist. Nobody cares or really ever cared you put stamps in the spot on envelope s

Re:Rust vs. C++ Formatting by scrubs on Monday January 16, 2023 @11:38AM (#34402377)
get with the program. Printf in it's 86 varieties has been around for years.

Second, I'm increasingly associating that brit 70s show "keeping up appearances" with rust specifically that overly blushed face of the woman protagonist. Nobody cares or really ever cared you put stamps in the spot on envelope s

Re:Rust vs. C++ Formatting by RcouF1uZ4gsC on Monday January 16, 2023 @03:52PM (#34405332)
Actually, in defense of C++, there are not very many statically typed, compiled languages that can implement a type-safe print function that takes a variable number of arguments (that do not all share a base class) using the language itself. For many languages, print is special and the implementation is provided as part of the language implementation.

ADDENDUM:

Even iostreams back in the 1980's was a huge deal at the time, as it showed that it was possible to implement type-safe io in a statically typed, compiled language using language features just like any other library, instead of having it be special purpose.

Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @04:54PM (#34405979)
huh? Every new language has type safe print functions. Having to specify the type with printf formatters is archaic and awful.
Re:Rust vs. C++ Formatting by kccqzy on Monday January 16, 2023 @05:12PM (#34406144)
Most new languages with type-safe print functions are either dynamically typed (which makes this whole discussion moot) or leverage some significant advance in PL theory in order to make the print function type-safe. By advance, I mean not known before the 1990s.
Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @05:46PM (#34406524)
Sorry, I explicitly meant statically typed languages. Rust, Nim, Zig, Odin, Jai, etc.

> For many languages, print is special and the implementation is provided as part of the language implementation.

Sure. Printing variables to strings is definitely worth language-level features imho. It's a bad thing if a language (C++) requires users to come up with extremely complex libraries (fmt/std::format) because the language lacks the features to make such a common operation simple and reliable.

C style printf is a dumpster fire. C++ iostreams are unuseably slow. Modern languages definitely solve this particular problem much better!

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @06:30PM (#34407014)
> Sorry, I explicitly meant statically typed languages. Rust, Nim, Zig, Odin, Jai, etc.

Requiring a macro is no better than what C does. Most of those languages have a less awful macro system than C's, but that's not an actual language advancement. The state of printing is still awful in the languages you mention.

Re:Rust vs. C++ Formatting by dilap on Monday January 16, 2023 @06:57PM (#34407250)
You don't like Zig's solution? I think it's excellent. Normal zig code, executed at compile-time. No magic, no macros, fully compile-time error-checked formatting.

https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L...

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @08:22PM (#34407996)
I don't, no. Proper stage polymorphism is great, but running arbitrary code at compile time without a coherent model of how you're doing that (which ad-hoc const/constexpr/etc. isn't) is like substituting for generics by having dynamic types. If I'm just going to run arbitrary code at compile time I might as well do that with a make rule.
Re:Rust vs. C++ Formatting by dilap on Tuesday January 17, 2023 @08:33AM (#34412160)
Sure, it is basically a dynamic language at compile time, but I feel like the downsides of that are significantly mitigated by the fact that it's running at, you know, compile time -- you'll still get compile-time errors for any mistakes!

(Some people even try to write entire programs in dynamic languages, as crazy as that is.)

Regarding "might as well have a makefile rule," it's not really fair -- in practical terms having a seemless way to evaluate functions in your code at compile time is way different than doing codegen. Like, in practice, how would you write a type-safe format function using codegen? You could do it, but it would be very, very gnarly. Very different.

Even Go -- which famously for a while tried to advocate codegen as an alternative to generics (w/ stuff like stringer to codegen print functions for human names for constants) -- resorts to dynamic types & runtime errors for formatting.

So let's see:

formatting approaches we've seen so far:

– dynamic w/ runtime errors (e.g. Go, C#, C, ...)

– static via baked into compiler support

– static via special-case restricted compile-time evaluated language (e.g. rust macros)

- static via full-lang compile-time eval (e.g. zig)

I think you said all those are awful, so, I'm curious -- what's the better approach you have in mind?

Re:Rust vs. C++ Formatting by lmm on Tuesday January 17, 2023 @05:07PM (#34420094)
IMO the least-bad vaguely mainstream approach is what people do in Haskell with e.g. formatting or fmt. Your formatter should be a first-class strongly typed value, and the way you form/construct that value should be the normal way you form values in your language.

I do think there's space for a "write a complex value literal by writing a string in a DSL and embedding that into the source" feature. But that shouldn't be specific to format strings, and it shouldn't be by running arbitrary code at compile time; rather it should be a language feature. I haven't seen a version of that that I'm really happy with yet, but Haskell's OverloadedStrings or Scala's StringContexts are some baby steps in the right direction.

Re:Rust vs. C++ Formatting by dilap on Wednesday January 18, 2023 @08:19AM (#34426286)
Thanks.

My haskell knowledge is minimal, but looking at formatting and fmt library examples, what stands out is you're operating in the syntax of the language, so it's clumsier to use (imo) than a string with interpolations. (It seems not-dissimilar to C++ streams, to me.)

I guess your second paragraph gets at that. I think you're arguing for, "build it into the language, but flexibly." Fair enough!

Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @07:09PM (#34407350)
> Requiring a macro is no better than what C does.

I disagree in the strongest, most emphatic way possible.

As a user of programming languages the difference between printing in C and printing in Rust is night and day. Whether that is achieved with a powerful macro system or with the core language does not matter to me in the slightest. It carries zero weight.

Writing code that prints variables in C is painful and bad. Writing code that prints variables in Rust is easy and good. If you'd like to say that "easy and good" is the same as "painful and bad" then I disagree.

When std::format ships the gap between C++ and Rust will shrink dramatically. However Rust will still have an advantage with derive macros. C++'s lack of reflection continues to be a major pain point. Maybe in C++29.

I don't think you can fully separate "language" from "macros". If something can be implemented easily in a macro then there is less reason to bake it into a new feature in the language. I don't think it's a good idea to add language features solely so you can say it's a language feature and not a macro feature. YMMV.

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @11:53PM (#34409289)
A language that makes it easy to represent how to process a potentially complex but semi ad-hoc structure in a coherent way is interesting. A language that's added a special case for when the processing you want to do happens to be printing is much less so. Derive is a poor substitute for a proper record system.
Re:Rust vs. C++ Formatting by forrestthewoods on Tuesday January 17, 2023 @01:33AM (#34409715)
Maybe. But a poor substitute is radically superior alternative to literally nothing.
Re:Rust vs. C++ Formatting by int_19h on Monday January 16, 2023 @07:37PM (#34407614)
If the result is type safe and the syntax is consistent with the rest of the language, the users won't and shouldn't care whether it's implemented as a builtin, as a macro, or as some other kind of magic.
Re:Rust vs. C++ Formatting by xigoi on Tuesday January 17, 2023 @01:36AM (#34409724)
Nim's implementation doesn't require a macro.
Re:Rust vs. C++ Formatting by atq2119 on Monday January 16, 2023 @06:02PM (#34406700)
Rust's print! implementation is pretty darn complicated, wouldn't you say?
Re:Rust vs. C++ Formatting by xigoi on Monday January 16, 2023 @06:17PM (#34406864)
Re:Rust vs. C++ Formatting by elcritch on Monday January 16, 2023 @06:36PM (#34407063)
The magic that makes Nim’s print / repr functions really neat is the `fieldPairs` iterator that works on any type. It’s a comptime function to iterate fields of a type, very handy and very simple to use. It can work on any type unlike Rust’s Debug macro which only works on types you own and can annotate.

https://nim-lang.org/docs/iterators.html#fieldPairs.i%2CS%2C...

Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @06:20PM (#34406901)
I don't know what Rust's print! implementation looks like. I also don't care. I am not a Programming Language programmer. I am a user of programming languages.

Using C-style printf sucks balls. It's extremely error prone and it doesn't support complex types. Using Rust's print system is delightful. I can't make a type error and arbitrarily complex types can be printed with roughly zero effort via {:?} and #[derive(Debug)].

Re:Rust vs. C++ Formatting by joenot443 on Monday January 16, 2023 @05:46PM (#34406521)
Swift can do it, yes?
Re:Rust vs. C++ Formatting by meindnoch on Monday January 16, 2023 @06:31PM (#34407026)
Swift can do what?

String interpolation is part of the language in Swift.

Re:Rust vs. C++ Formatting by dureuill on Monday January 16, 2023 @06:10PM (#34406787)
> For many languages, print is special and the implementation is provided as part of the language implementation.

what's the point of not having print provided as part of the language implementation, though?

Re:Rust vs. C++ Formatting by int_19h on Monday January 16, 2023 @07:39PM (#34407633)
Print is not the only facility where strongly typed variadic arguments are useful. If you implement it as a builtin, all those other cases will have to be implemented as a library, and as a result, argument passing looks very different from print. And it is desirable that the same thing is consistently expressed in the same manner.
Re:Rust vs. C++ Formatting by Karellen on Tuesday January 17, 2023 @07:22AM (#34411630)
Because plenty of places where C is used won't need it, or indeed any part of libc.

Also, it's a fairly simple to decide upon dividing line between the language and the library - if it can be implemented in a handful of assembly instructions (or, ideally, 1 instruction) it's part of the language. If it can't and needs a "function" (either in C, or implemented in assembly) to work, or needs to talk to some other part of the computer (like a kernel, or a BIOS) it's part of the library.

Like malloc()/free() - again, not part of the core language spec, but part of the standard library, which can be omitted in some ("freestanding", as opposed to "hosted") implementations.

Remember, C was created in the '70s. Memory was measured in kilobytes. Tens of kilobytes if you were lucky. Even through the '80s, 1 megabyte was a lot.

Re:Rust vs. C++ Formatting by tragomaskhalos on Tuesday January 17, 2023 @07:35AM (#34411717)
I always thought that iostreams was very elegant. But it wasn't until the internet came along that I learned that most of the rest of the world regards its use of `operator <<` as a crime against decency, so what do I know?
Re:Rust vs. C++ Formatting by stevenhuang on Monday January 16, 2023 @05:31PM (#34406339)
The format in C++20 is the 3rd party https://fmt.dev/latest/index.html , which means even c++11 projects can use it.
Re:Rust vs. C++ Formatting by andrepd on Monday January 16, 2023 @06:01PM (#34406687)
Rust always had printf. If you mean python-style fmt, there were also libraries for that for a long time, e.g. https://fmt.dev/
Re:Rust vs. C++ Formatting by kzrdude on Monday January 16, 2023 @06:07PM (#34406757)
after 30 years, Python also finally has printf:

world = "earth"

print(f"Hello {world}")

Re:Rust vs. C++ Formatting by mk89 on Monday January 16, 2023 @06:12PM (#34406811)
And it will take another 40 to convince people to use it instead of cout. :)
Re:Rust vs. C++ Formatting by renox on Tuesday January 17, 2023 @05:29AM (#34410963)
I doubt it very much: cout is so bad (ever tried to display an hexadecimal number? Barf!) that in many case printf is used instead even though it isn't typesafe. Which isn't so bad when you use a static checker in the CI, which you should anyway the C++ being so full of traps.
Re:Rust vs. C++ Formatting by taspeotis on Tuesday January 17, 2023 @05:13AM (#34410905)
How's the Networking TS coming along? I can't wait to connect to ARPANET.