100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

06_ticket_management/05_iter references ?

Open mdespriee opened this issue 1 year ago • 4 comments

I struggled to understanding the exercise. The directive is to provide an iter method that returns an iterator over &Ticket items.

The correct answer is

    pub fn iter(&self) -> std::slice::Iter<Ticket> {
        self.tickets.iter()
    }

How is that an iterator over &Ticket ? How can one understand that from the type itself ?

I don't find the Rust Book explicit on that topic, either. Nor the inline doc with Iter in my IDE. It might help to add a couple of lines in the page that goes with this exercise.

my2ct.

PS: thanks for your work, I enjoy very much this course so far.

mdespriee avatar Aug 31 '24 16:08 mdespriee

Hi @mdespriee,

The fn is returning a std::slice::Iter<T>, where T is a Ticket in this case. As you can see in the docs, this std::slice::Iter<T> implements the Iterator trait with Item = &T (there's a lifetime annotation (the 'a there as well, but let's skip over that right now).

Since in this case the T is a Ticket, std::slice::Iter<Ticket> implements Iterator with Item = &Ticket.

Does that make sense?

Your solution is great, but in my opinion the exercise lends itself to many solutions (e.g. returning impl Iterator<Item = &Ticket> or creating a custom Iterator), so I'm not convinced we should add this information to the page at this point

hdoordt avatar Sep 20 '24 07:09 hdoordt

hello, una pregunta, como haces para hacer un ticket, esa parte no lo entendi ayudame a entender quisiera hacerlo

SlayerPower avatar Oct 02 '24 00:10 SlayerPower

es algo increibel, osea jaja es increible este proyecto bro, chamo me ecanta esto peeee

SlayerPower avatar Oct 02 '24 00:10 SlayerPower

cuanto duraste en hacer esto¡¡??

SlayerPower avatar Oct 02 '24 00:10 SlayerPower

How can one understand that from the type itself ?

You need to check its documentation! In particular, this line.

LukeMathWalker avatar Oct 09 '24 07:10 LukeMathWalker