-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Discussed in #1269
Originally posted by njr0 September 27, 2023
I had a few questions/thoughts on this section.
- You don't really explain what the
type Item
is, unless I missed it. - I wondered if you'd be able to replace the
Self::Item
withu32
, but of course you can't. - 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.