Lifetime: cannot infer an appropriate lifetime due to conflicting requirements

Thanks for help me, I suffered from rust compile error:

cannot infer an appropriate lifetime due to conflicting requirements

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:26:9
   |
26 |         Some(self.s)
   |         ^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 24:5...
  --> src/main.rs:24:5
   |
24 | /     fn next(&mut self) -> Option<Self::Item> {
25 | |         //self.s.as_mut().map(|v| &mut (**v).s)
26 | |         Some(self.s)
27 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:26:14
   |
26 |         Some(self.s)
   |              ^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 20:6...
  --> src/main.rs:20:6
   |
20 | impl<'a> Iterator for IterMut<'a> {
   |      ^^
   = note: ...so that the types are compatible:
           expected std::iter::Iterator
              found std::iter::Iterator

error: aborting due to previous error

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:26:9
   |
26 |         Some(self.s)
   |         ^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 24:5...
  --> src/main.rs:24:5
   |
24 | /     fn next(&mut self) -> Option<Self::Item> {
25 | |         //self.s.as_mut().map(|v| &mut (**v).s)
26 | |         Some(self.s)
27 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:26:14
   |
26 |         Some(self.s)
   |              ^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 20:6...
  --> src/main.rs:20:6
   |
20 | impl<'a> Iterator for IterMut<'a> {
   |      ^^
   = note: ...so that the types are compatible:
           expected std::iter::Iterator
              found std::iter::Iterator

error: aborting due to previous error

for

#[derive(Debug)]
struct IString {
    s: String,
    n: OBString,
}
type OBString = Option<Box<IString>>;

impl IString {
    pub fn new(s: String) -> IString {
        IString { s: s, n: None }
    }
    pub fn iter_mut(&mut self) -> IterMut<'_> {
        IterMut { s: &mut self.n }
    }
}

struct IterMut<'a> {
    s: &'a mut OBString,
}
impl<'a> Iterator for IterMut<'a> {
    type Item = &'a mut String;
    // how to return ?
    // Please make a pull request or make a issue to help me fix this.
    fn next(&mut self) -> Option<Self::Item> {
        self.s.as_mut().map(|v| &mut (**v).s)
    }
}

fn main() {
    let mut v1 = Some(Box::new(IString::new(String::from("a"))));

    for v in v1.iter_mut() {
        println!("v={:?}", v);
        *v = format!("11{}",*v);
        println!("v={:?}", v);
    }
}

Also you can make comment here https://github.com/fooofei/help/blob/master/qa1/src/main.rs

This implementation is undefined behavior and it is actually dangerous.

Iterator must never ever return the same mutable (exclusive) reference more than once.

By contract of the iterator interface, this is legal:

let a = iter.next();
let b = iter.next();
drop(iter);
use_both(a,b);

and in Rust it's UB if a and b were exclusive references pointing to the same memory.


If your actual code does something else, and never reuses the same mutable reference, then it may be OK. And you may need to use unsafe to cast mutable references, because it's often impossible to convince the borrow checker that the mutable references are really unique.

Thanks a lot.