'anon struct' or 'named tuple fields'?

I really want this – so macro black magic is acceptable.

Is there a way to either have:

  • ‘anonymous’ structs or
  • tuples where fields have names instead of .0, .1, .2, … ?

The main use for this is that sometimes, I want to create lots of intermediate ‘types’ without giving names to each of the type, but still reap the benefits of static type checking.

Rust actually used to have this feature, referred to as ‘structural records’, but they were removed in 0.6 - some context of why they were removed can be found on this internals thread. There was the beginnings of a pre-RFC to bring them back, but that conversation seems to have died out a bit.

Doesn’t directly answer your question, but hopefully it’ll give you a bit of context for why this isn’t part of the language right now!

Anonymous objects are generally when you don’t care about a type and often use it once rather than “storing it” which might be difficult with type checking Also it even if you had an anonymous struct it would be difficult to specify to a function about how you are expecting to receive any anonymous struct.

You can create a macro that “kind of” provides anonymous struct, but unfortunately, each macro invocation produces a totally different type.

If O(log n) rather than O(1) access times are acceptable then what you want is likely a HashMap or BTreeMap.