Hacker News new | past | comments | ask | show | jobs | submit login

IOW I believe it's the same thing as Rust's format_args! macro, but trying to get away without needing a separate format! macro by using implicit conversions.





std::format_args! gets you a Arguments<'a> which we'll note means it has an associated lifetime.

Today-I-learned, Arguments<'a> has a single useful function, which appeared before I learned Rust but only very recently became usable in compile time constants, as_str() -> Option<&'static str>

format_args!("Boo!").as_str() is Some("Boo!")

If you format a literal, this always works, if you format some non-literal the compiler might realise the answer is a compile time fixed string anyway and give you that string, but it might not even if you think it should and no promises are given.


The most useful function is Arguments::fmt. “as_str” is just a shortcut utility function.

But there is no Arguments::fmt ? Are you thinking of the implementations of Debug::fmt and Display::fmt on Arguments ? Trait implementation isn't the same kind of thing at all.

There is exactly one useful thing you can do with an `Arguments` object: call `.fmt()` on it.

The whole reason for std::Arguments very existence is to call `std::Arguments::fmt` on it.

`.fmt()` is a trait implementation, but that doesn’t change anything (not sure what “kind of thing” refers to here). It’s still a function on std::Arguments.


I think you're quite muddled about what's going on here

The full name of this type is std::fmt::Arguments not std::Arguments and even so there's no such thing as std::fmt::Arguments::fmt - there is no function with that name, we can only talk about this name (since it doesn't exist) if we bring into context a specific trait such as Display or Debug

So the full name of the thing you think is the "one useful thing you can do with Arguments" is

<std::fmt::Arguments as std::fmt::Display>::fmt

or perhaps it's

<std::fmt::Arguments as std::fmt::Debug>::fmt

... as I said, Arguments implements both traits, and their sole function has the same name so we need to disambiguate somehow if we mean one of these functions or the other. For the function defined on Arguments itself, as_str, it's already unambiguous.

In the end the Debug and Display traits are all just ductwork, which is why as_str caught my attention.


It is not a shortcut because it can't be implemented without knowing the `Arguments` internals. `format_args!("{}", "boo").as_str()` returns None for example.

It’s a shortcut in the sense that most, if not all optimisations are shortcuts. This one allows you to shortcut the usual formatting machinery if the result of formatting is a static string.

Like all shortcuts, it’s not something you can always rely on.


It can be used to shortcut the formatting process, the function itself is however not a shortcut in my opinion.

Interesting viewpoint: I see this as a distinction without a difference. I’m interested to know why you see it differently? What is its use, if not as a shortcut?



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: