Hello. Can you please help me to fix an issue in the bellow code. This is a simplified version of a code that I need to implement in my project.
struct StrHolder<'str> {
text: String,
ptrToText: Option<&'str String>,
}
impl <'str> StrHolder<'str> {
fn setPtr(& mut self) {
self.ptrToText = Option::Some(&self.text);
}
}
fn main() {
let mut holder = StrHolder {
text: String::from("asdada"),
ptrToText: Option::None,
};
holder.setPtr();
println!("{}", holder.ptrToText.unwrap())
}
so I have the method ''setPtr' which does some computation with a text and store a result as a pointer into the ptrToText.
The below code is not compiled with error "assignment requires that '1
must outlive 'str
"
I can change method to "fn setPtr(&'str mut self)" but in this case I receive error in the line "println!("{}", holder.ptrToText.unwrap())"
^^^^^^^^^^^^^^^^
| |
| use of borrowed holder
| borrow later used here
I wonder if it is possible to fix this code. Thanks in advance