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 help/main.rs at master · fooofei/help · GitHub