How to introspect all available methods and members of a Rust type?

前端 未结 6 609
心在旅途
心在旅途 2020-12-11 00:33

Is there a way to print out a complete list of available members of a type or instance in Rust?

For example:

  • In Python, I can use print(dir(objec
6条回答
  •  悲哀的现实
    2020-12-11 01:12

    If you need the field names inside your program then you probably need to use macros. Either wrap your struct definition in macro and pattern match to create some function to get their names, or use procedural macro to derive structs for traits with such functions.

    See examples in syn for derived traits. In particular, see syn::Data::Struct which has fields.

提交回复
热议问题