I stumbled on something I was not expecting when using the Chars
type. I called, the .next()
method and found out it was mutating the original char values.
To illustrate in code and to compare with calling next
on a vec:
let mut c: Chars = "hello".chars();
dbg!(c.next()); // prints Some("h")
dbg!(c); // prints Chars(['e','l','l','o',])
As you can see, after calling next
the h
is gone, and the c
value is now 'e','l','l','o'
Whereas with vec, that is not the case:
let v1 = vec![1, 2, 3];
let mut v1_iter = v1.iter();
dbg!(v1_iter.next()); // prints Some(1)
dbg!(v1); // prints [1,2,3]
As can be seen, calling next
does not mutate v1
to remove the 1
element.
Why is this the case with Chars
? Is it demonstrating a well defined characteristics for some type of iterators in rust that I am not aware of? That it iterators where iterating actually consumes and mutates the original value being iterated?