I have successfully implemented a custom numeric base system in Rust via linked lists called digits.
pub struct Digits<'a> {
mapping: &'a BaseCustom<char>,
digit: u64,
left: Option<Box<Digits<'a>>>,
}
I've declared a lifetime on the linked list struct and linked it directly to an instance of BaseCustom
. When I reassign half of the linked list, what happens to the unreferenced chunk which still has its lifetime associated with the BaseCustom
mapping?
For example, I have a linked list that looks like "hello" (I'll use left to right for this example and not right to left as in my project)
h -> e -> l -> l -> o
Then I reassign the linked list reference from e
to a different set of characters.
h -> e l -> l -> o
\
-> d -> g-> e
Now that the code is no longer using the "llo" of hello, does that memory automatically get freed? Does the fact that each character instance here have a reference to BaseCustom
's lifetime mean that the memory is held on to until the program ends?
I know Rust doesn't have or use a garbage collector so the lifetime reference to BaseCustom
confuses me here. Is it stating that the items must live as long as BaseCustom
? Or is it that they get freed up at some point but BaseCustom
must outlive them?