Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That seems strange to me. If calling max() changed the sequence, I would expect it would have to be declared as mutable.

I struggled with this when writing my first rust program. I was very surprised by the behaviour.



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=50e6fe86e530baeabda1a7cca3c...


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: