Why the documentation doesn't have mut while the source code has

I'm learning closures, and it is stated that FnMut parameter must be declared mut. One example I find is the fold method of iterator. But when I look up the documentation, the closure parameter doesn't have mut (Iterator in std::iter - Rust) while the linked source code has mut (iterator.rs - source), and I notice the same thing for the self parameter. I'm a bit confused. Is it a bug in documentation or is it a convention to elide some finer details? As rust is new to me, I would prefer the documentation being more precise.

This is deliberate. The mut you're referring to is a property of the binding, not what you're passing in (so it's not the same as &T versus &mut T). The caller doesn't care what is happening within the method, and in fact you could omit the mut on the binding and write let mut this = self; in the body if you so desired.

3 Likes

Thanks, that's interesting. Is it because the parameter is moved in rather than referenced, so I can make it mutable later (i.e. the mutability doesn't matter at the binding)?

It doesn't matter to the caller, right. If you can move a value, you can move it from a non-mut binding to a mut binding. So whether or not the binding in the implementation signature was mut or not is irrelevant to what the body may or may not do to the value.

The documentation showing every binding as mut would be another viable alternative. Arguably more reflective of the function's capabilities (but also more noisy).

2 Likes