Lifetime error when pushing value into a generic type

⚓ Rust    📅 2025-12-11    👤 surdeus    👁️ 3      

surdeus

How can I avoid this lifetime error:

error[E0597]: `s` does not live long enough

Storage doesn't have a lifetime parameter, so s is not used after storage.push(&s);

trait PushValue<T> {
    fn push(&mut self, value: T);
}

fn push_something<'a, S: PushValue<&'a str>>(storage: &mut S) {
    let s = format!("something {}", 234);
    storage.push(&s);
}

// struct DefaultStorage {
//     string: String
// }

// impl<'a> PushValue<&'a str> for DefaultStorage {
//     fn push(&mut self, value: &'a str) {
//         self.string.push_str(value);
//     }
// }

Playground link: Rust Playground

3 posts - 2 participants

Read full topic

🏷️ Rust_feed