100-exercises-to-learn-rust
100-exercises-to-learn-rust copied to clipboard
06_ticket_management/05_iter references ?
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.
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
hello, una pregunta, como haces para hacer un ticket, esa parte no lo entendi ayudame a entender quisiera hacerlo
es algo increibel, osea jaja es increible este proyecto bro, chamo me ecanta esto peeee
cuanto duraste en hacer esto¡¡??
How can one understand that from the type itself ?
You need to check its documentation! In particular, this line.