I can't make the code below compile. Neither can't figure out keywords to search through the forum since I'm still new to Rust.
#![allow(dead_code)]
struct Holder<'a> {
text: &'a mut String,
}
impl<'a> Holder<'a> {
fn new(text: &'a mut String) -> Self {
Holder { text }
}
fn append(&mut self, suffix: &str) -> () {
self.text.push_str(suffix);
}
}
struct SuperHolder<'a> {
sub_holder: &'a mut Holder<'a>,
}
impl<'a> SuperHolder<'a> {
fn new(text: &'a mut String) -> SuperHolder<'a> {
SuperHolder {
sub_holder: &mut Holder::new(text),
}
}
}
fn main() {
let mut text = String::from("Hello");
let mut holder = Holder::new(&mut text);
holder.append(" world");
println!("{}", holder.text);
}
Errors:
21 | sub_holder: &mut Holder::new(text),
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
22 | }
23 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 18:6...
--> src/lib.rs:18:6
|
18 | impl<'a> SuperHolder<'a> {
| ^^
I understand the error but I don't know what to do with it. Neither &'mut Holder::new(text)
, nor &mut Holder<'a>::new(text)
compile.