Learning Rust - Fibonacci position picker

The only thing that stands out to me in your current code is that you should detect when the user enters "0" as well.

I also think this would be a great opportunity to learn about implementing your own Iterator. If you start with something as simple as this:

struct Fibonacci {

impl Iterator for Fibonacci {

The compile will tell you what's missing, and you can add fields to the struct and items to the implementation until it works. Then you could use it something like

let iterator = /* ... create a Fibonacci somehow ... */;
let value = iterator
    .nth(position - 1)
    .expect("the position was unexpectedly too large");

Sweet, thanks for the pointer. Especially the hint about letting the compiler walk me through what I need. That sounds awesome for learning. I'll try it out!

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.