As I get more familiar with Rust, this happens less and less, but last night, I had a good fight with the borrow cheker again.
I am developing an RCON client for which I now implemented a proxy iterator over players on the server. Each proxy object contains the player data and a mutable reference to the underlying RCON client.
Here's the implementation I managed to write, that compiles:
However, I'd prefer to not have next() as a struct method, since it makes using the proxy iterator weird (while let Some(player_proxy) = players_mut.next() {}).
Is there a chance to implement this with the Iterator trait? I failed miserably in trying to do so, because of lifetime bounds. I suspect, that I might need a so-called lending iterator for which afaik no standad library trait exists yet, but I'm not sure about that.
Yes, that's what you would need. All items from a (non-lending) Iterator can be held on to at the same time, e.g. collected, but it's UB to have multiple &mut to the same place active at the same time. So it can't handle this "hand out &mut * reborrows" pattern.
Until there's language support, a lending iterator (from a crate or not) has to use while let, not for. So that part is to be expected.