I am learning Rust from a C++/Java background, and I have the following pattern
struct Node<'a> {
network_manager: NetworkManager<'a>,
}
struct NetworkManager<'a> {
base_node: &'a Node<'a>,
}
The node contains the threadpool that the NetworkManager
uses to "handoff" messages once they've been processed. Because of the recursive call, it is not possible to set the base_node field in the NetworkManager
immediately. In Java, I would leave it as null and have a second method that is called after the constructor called initialise(BaseNode node)
that would set the base_node
field (ensuring that there are no calls to the network manager before initialise
is called).
What is the idiomatic way of doing this in Rust? The only way I can think of is to make base_node
an Option
type, but this seems suboptimal.
In general, what is the "right" way in Rust to deal with situations where A points to B and B points to A, and where (as in my case), refactoring is not possible?