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

前端 未结 6 610
心在旅途
心在旅途 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:28

    To expand on my comment, you can use rustdoc, the Rust documentation generator, to view almost everything you're asking for (at compile time). rustdoc will show:

    • Structs (including public members and their impl blocks)
    • Enums
    • Traits
    • Functions
    • Any documentation comments written by the crate author with /// or //!.

    rustdoc also automatically links to the source of each file in the [src] link.

    Here is an example of the output of rustdoc.

    Standard Library

    The standard library API reference is available here and is available for anything in the std namespace.

    Crates

    You can get documentation for any crate available on crates.io on docs.rs. This automatically generates documentation for each crate every time it is released on crates.io.

    Your Project

    You can generate documentation for your project with Cargo, like so:

    cargo doc
    

    This will also automatically generate documentation for your dependencies (but not the standard library).

提交回复
热议问题