I'm developing my real first project in Rust and I stumbled upon lifetimes.
Here's the code to describe my scenario:
struct A<'a> {
ref_to_something: &'a String
}
impl<'a> A<'a> {
pub fn new(s: &'a String) -> anyhow::Result<A> {
Ok(A {
ref_to_something: s
})
}
}
struct B<'a> {
something: String,
list_of_a: Vec<A<'a>>,
}
impl<'a> B<'a> {
pub fn new() -> B<'a> {
B {
something: "hello".into(),
list_of_a: vec![],
}
}
pub fn add(&mut self) {
if let Ok(a) = A::new(&self.something) {
self.list_of_a.push(a);
}
}
}
The choice of String is arbitrary, it can be any type (in my case it was a struct). A
needs a reference to something in order to be initialized and that something is specifically a member of B
. On the other hand, B
has a list of A
s. To add to the list, I need to pass the reference to my something
which resides in B
.
But for the function B::add
Rust is telling me that I need to specify the lifetime of self
, because there's a chance that self
doesn't live long enough. Since I had this error I took a different approach to the problem (I'm not referencing that something
in A
anymore), but I wonder if I could have achieved what I wanted using the references as shown above or it simply a bad design choice on my part.