Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better example in "Iterator" slide #1350

Open
djmitche opened this issue Oct 11, 2023 Discussed in #1269 · 1 comment
Open

Better example in "Iterator" slide #1350

djmitche opened this issue Oct 11, 2023 Discussed in #1269 · 1 comment

Comments

@djmitche
Copy link
Collaborator

Discussed in #1269

Originally posted by njr0 September 27, 2023
I had a few questions/thoughts on this section.

  1. You don't really explain what the type Item is, unless I missed it.
  2. I wondered if you'd be able to replace the Self::Item with u32, but of course you can't.
  3. The take(5) is obviously to prevent overflow, but I think it's more interesting without that. At least, I think you might suggest the instructor or student removes it. It doesn't take too long to run and the output is great:
   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
     Running `target/debug/playground`
thread 'main' panicked at 'attempt to add with overflow', src/main.rs:10:24
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fib(0): 1
fib(1): 1
fib(2): 2
fib(3): 3
fib(4): 5
fib(5): 8
fib(6): 13
fib(7): 21
fib(8): 34
fib(9): 55
fib(10): 89
fib(11): 144
fib(12): 233
fib(13): 377
fib(14): 610
fib(15): 987
fib(16): 1597
fib(17): 2584
fib(18): 4181
fib(19): 6765
fib(20): 10946
fib(21): 17711
fib(22): 28657
fib(23): 46368
fib(24): 75025
fib(25): 121393
fib(26): 196418
fib(27): 317811
fib(28): 514229
fib(29): 832040
fib(30): 1346269
fib(31): 2178309
fib(32): 3524578
fib(33): 5702887
fib(34): 9227465
fib(35): 14930352
fib(36): 24157817
fib(37): 39088169
fib(38): 63245986
fib(39): 102334155
fib(40): 165580141
fib(41): 267914296
fib(42): 433494437
fib(43): 701408733
fib(44): 1134903170
fib(45): 1836311903

I did think, however, that while this is an interesting iterator, it's a slightly weird example, being "apparently" infinite but actually only only capable of producing 46 results before overflowing. Would it be better to handle the overflow and terminate iteration when you get past the capacity of an u32? (The implementation is slightly weird too, in the it overflows one sooner than it needs to. 1,134,903,170 + 1,836,311,903 is obviously less than 4 billion (2,971,215,073) and is already stored in next.

It also feels like a slightly odd example in that .next() mutates the item in a pretty fundamental way. But maybe that's not usual? Not sure. It felt like a slightly weird example to me.

@djmitche
Copy link
Collaborator Author

I think this is a good example of an iterator that is not over a collection. Such an iterator typically will mutate itself in next (most iterators do). But, we have covered collections already, so perhaps we should make some "interesting" iterator over a Vec or something like that?

The overflow conversation is an interesting one, and sounds like a good # More to Explore section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant