How to read crates docs? (find path to the module)

It may be silly, but as I learn Rust, the biggest problem for me is to read docs for modules. One of the issues, that I can't construct proper use path to the object to use it.

Without concrete example it sounds silly, so here it is:

I have a piece of code which uses piston library. The have events iterator which return event, which, according to docs, is a simple enum:

I have an event loop, and when I do debug print for events, I see exactly what was promised in docs: Render, PostRender, Idle, etc.

But when I try to do pattern matching on those, I just can't spell the path to things I want to match:
Docs says: piston::event::Event
I tried:

use piston;
match e {
   piston::event::Event => {println!("here");}

and got "could not find event in piston".
I've tried to write

use piston::event

but got the same error.
I've tried to break code to see what type it expects:

77 |                 1 => {println!("here");},
   |                 ^ expected enum `piston_window::Event`, found integer

I've tried to use this thing, but error is confusing:

77 |                 piston_window::Event::Render => {println!("here");},
   |                                       ^^^^^^ variant or associated item not found in `piston_window::Event`

Docs for piston_window are different:

and it's not the things I see in debug output:

            match e {
                _ => {println!("{:?}", e);}

Example of output is:

Update(UpdateArgs { dt: 0.008333333333333333 })
Idle(IdleArgs { dt: 0.00094002 })
Render(RenderArgs { ext_dt: 0.001123099, window_size: [800.0, 600.0], draw_size: [800, 600] })
Idle(IdleArgs { dt: 0.005773664 })

As you can see, it's not the piston_window::Event, it looks like piston::event::Event, but I can't use it.

I'm completely lost. Either I don't know how to interpret docs, or something strange is there...

When matching on an enum, your patterns need to have the same structure as the thing you're matching on. For example, look at the docs for Event::Render:


Your pattern needs to look the same:

match e {
    Event::Render(render_args) => {
        // you can do something with the `render_args`here

Here's some other examples:

enum Example {
    StructVariant { field_name: i32 },

match e {
    Example::UnitVariant => { ... },
    Example::TupleVariant(i) => { ... },
    Example::StructVariant { field_name } => { ... },

I would recommend reading chapter 6 and chapter 18 of the book if you've not done so already, they go into further detail on this.

The docs on are very old. (Probably two of more years) Try instead. You can prefix the name of any crate on with to get the documentation.

1 Like

Ah, yeah, Event is in the root of the crate in the current version:

So the path would be piston::Event.

Also, the debug output is confusing in this case, because it's logging out the nested data within the Event, rather than the Event itself:

impl fmt::Debug for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Event::Input(ref input, _) => write!(f, "{:?}", input),
            Event::Loop(ref l) => write!(f, "{:?}", l),
            Event::Custom(ref id, _, _) => write!(f, "Custom({:?}, _)", id),

So you're seeing Loop events in your output, even though the thing you're printing is an Event, which is massively confusing.

I have no idea why someone would choose to do this rather than just deriving Debug :grimacing:

Thank you everyone for help!

This code does what I want (at a minimum level):

match e{
    piston::Event::Input(ref input, ts) => {
        println!("Input ts:{:?} input:{:?}", ts, &input);
    _ => {},

Few key insights:

  1. There is a huge difference between and (very much unexpected for me). Path in a module is straightforward, but one need to look for the version.
  2. Events was heavily redone inside Piston whilst keeping debug output intact.

(And I've just learned to use ref inside match because Input does not support Copy trait).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.