4 ms·
If calling max() changed the sequence, max does not change a sequence, it consumes the iterator. I would expect it would have to be declared as mutable max t
by danieldk 8y ago
If calling max() changed the sequence,
max does not change a sequence, it consumes the iterator.
I would expect it would have to be declared as mutable
max takes self (and not &self), so the ownership of the iterator is moved to the max method. The new owner can decide to bind the value mutably. This is always the case when a value is moved and changes ownership, e.g.:
https://play.rust-lang.org/?gist=50e6fe86e530baeabda1a7cca3c785ab&version=stable&mode=debug&edition=2015 https://play.rust-lang.org/?gist=50e6fe86e530baeabda1a7cca3c...
- slavik81 8y agoI suppose designing it that way still prevents you from doing anything wrong, but you need quite a lot of knowledge of the system to understand what it's doing. I'll get there one day, but it's hard to go back to having no idea what my compiler is trying to do.