user
user

Reputation: 5370

List all traits implemented by a type in a scope

For the sake of making debugging easier and such, I would like to know all traits implemented for a type within a certain scope. Can I get rustc to provide me this information? If so, how?

Upvotes: 8

Views: 1226

Answers (2)

Aaron
Aaron

Reputation: 7030

Here's a janky method for getting this information during development, using autocomplete (I came here hoping for something a little more robust than autocomplete, but have not found it yet).

Using a JetBrains IDE with the rust plugin, within the desired scope, I can pull up an autocomplete list of fields/methods for any type by typing:

<NameOfType>::

List of autocomplete options for "<&mut i32>::"

The traits they implement will be listed alongside the methods when applicable: Autocomplete suggestion that reads "borrow(&self) of Borrow"

So, you can scroll through that list and hopefully locate the traits you're looking for. Not great, but works in a pinch.

Upvotes: 1

mcarton
mcarton

Reputation: 29981

Use rustdoc/cargo doc.

rustdoc creates a section with all trait implementations for a given type. For example with Vec:

<code>Vec</code> example

If you would like to do this for your own crate, you might find --document-private-items to be useful. See also How to generate documentation for private items.

Upvotes: 1

Related Questions