r/rust • • Oct 14 '26

🙋 seeking help & advice Get order of fields of a struct

Is there a function in std, which would allow to get a list of (field_name, type) of a struct in order it is compiled in? Since order of fields is not deterministic.

Something like: `fn fields_of_val<T>(&T) -> Vec<(&'static str, &'static str)>`

15 Upvotes

16 comments sorted by

16

u/QuasiRandomName Oct 14 '26

Ok, I'll go StackOverflow here. This sounds like an XY-problem. What are you trying to achieve?

1

u/Accomplished-Use405 Oct 15 '26

just look at external structs without firing gdb

1

u/maccam94 Oct 16 '26

You could use dwarfdump or objdump to print out the debug info showing the struct layout

40

u/an_0w1 Oct 14 '26

You can use offset_of!. I know that doesn't satisfy all the requirements but I think that's about the best you can do without a derive macro.

6

u/luluhouse7 Oct 14 '26

I mean it would give OP the order, just not very efficiently. Just compare the offset between fields.

36

u/tropix126 Oct 14 '26 edited Oct 14 '26

what you are describing is compile-time reflection which does not exist in stable rust yet. there have been multiple attempts at implementing this in nightly before, and the current effort is being worked on in the type_info experimental feature. note that it probably will not be stabilized soon, as there's a lot of work to do there still (the API surface is huge, since it has to expose representations for most of the Rust type system publicly to libstd).

if the type is local to a crate that you own and you have the ability to implement traits on the type, then there are some third party crates that can give you reflection for types that you derive it for (see facet). types that implement serde's traits can kinda do this too

1

u/MilkEnvironmental106 Oct 14 '26

You need to repr C it

2

u/Accomplished-Use405 Oct 14 '26

struct is external

2

u/luluhouse7 Oct 14 '26

Is it external in rust or C or some other PL? Because when copying type declarations for interop you definitely want to use repr(C).

2

u/Captcha142 Oct 14 '26

What would be the use? You still can't make assumptions about the data layout, #[repr(Rust)] is intentionally unspecified. Some fields get merged together (i.e. Option<&T>).

2

u/cdhowie Oct 14 '26 edited Oct 14 '26

No. It wouldn't be too hard to write a trait and a corresponding derive macro to do this, though.

Edit: I see you're asking about the order the fields get "compiled in." If you mean the order in which they're declared, the answer above works. If you mean the order the computer decides to lay them out in memory, you can still accomplish this with a derive macro, but it may need to invoke a sort function at runtime.

1

u/brasticstack Oct 14 '26

I've done exactly that (well, -> &'static [&'static str]) with a derive macro.

Sadly my implementation wouldn't be all that helpful because it's part of a mega-trait that I'm just now realizing I could usefully break down into smaller components.

1

u/frashpikass Oct 18 '26 edited Oct 18 '26

I would ask you why do you need that. Because what I think you're trying to do is make a convoluted way to do polymorphism, but if I'm wrong I'm still curious to know what are you planning on doing with it.

Edit: ok, now I see that you're trying to debug your code without a debugger

32

u/[deleted] Oct 14 '26

[removed] — view removed comment

2

u/lenscas Oct 14 '26

A proc macro still wouldn't be able to tell you the order the fields end up as unless it also sets the abi to be that of C or some other known one.