Implement iter_mut on custom type
⚓ Rust 📅 2025-09-25 👤 surdeus 👁️ 15I've gotten stuck with trying to implement iter_mut() on my custom type and haven't been able to find answers online. I've got it working in a similar way with immutable iter() which doesn't seem to work with making it mutable.
struct ChannelConfig{
data: u32,
channel: Channel,
etc...
}
struct Channels {
pub a: ChannelConfig,
pub b: ChannelConfig,
pub c: ChannelConfig,
pub d: ChannelConfig,
}
struct MyIterMut<'a> {
channels: &'a mut Channels,
next_chan: Option<Channel>,
}
impl<'a> Iterator for MyIterMut<'a> {
type Item = &'a mut ChannelConfig;
fn next(&mut self) -> Option<Self::Item> {
match self.next_chan {
Some(Channel::A) => {
self.next_chan = Some(Channel::B);
let chan_config = &mut self.channels.a;
Some(chan_config)
}
Some(Channel::B) => {
self.next_chan = Some(Channel::C);
let chan_config = &mut self.channels.b;
Some(chan_config)
}
Some(Channel::C) => {
self.next_chan = Some(Channel::D);
let chan_config = &mut self.channels.c;
Some(chan_config)
}
Some(Channel::D) => {
self.next_chan = None;
let chan_config = &mut self.channels.d;
Some(chan_config) //<----- Lifetime may not live long enough
}
None => None,
}
}
}
I've made an iterator struct (MyIterMut) to hold a mutable reference to the struct I'm wanting to iterate over and labelled the lifetime of my Channels struct but I still get the lifetime error and can't solve it:
error: lifetime may not live long enough
--> src\foo.rs:104:17
|
81 | impl<'a> Iterator for MyIterMut<'a> {
| -- lifetime `'a` defined here
...
84 | fn next(&mut self) -> Option<Self::Item> {
| - let's call the lifetime of this reference `'1`
...
104 | Some(chan_config)
| ^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
I think I understand that it's saying I'm returning a reference with the lifetime of the MyIterMut struct and not the ChannelConfig struct itself. How do I properly specify this lifetime or return it properly such that it has the correct lifetime?
Thanks!
3 posts - 3 participants
🏷️ Rust_feed