I'm quite new to Rust programming, and I'm trying to convert a code that I had in js to Rust.
A plain concept of it is as below:
fn main() { let mut ds=DataSource::new(); let mut pp =Processor::new(&mut ds); } struct DataSource { st2r: Option<&Processor>, } struct Processor { st1r: &DataSource, } impl DataSource { pub fn new() -> Self { DataSource { st2r: None, } } } impl Processor { pub fn new(ds: &mut DataSource) -> Self { let pp = Processor { st1r: ds, }; ds.st2r = Some(&pp); pp } } As you can see I have two main modules in my system that are inter-connected to each other and I need a reference of each in another.
Well, this code would complain about lifetimes and such stuff, of course 😑. So I started throwing lifetime specifiers around like a madman and even after all that, it still complains that in "Processor::new" I can't return something that has been borrowed. Legit. But I can't find any solution around it! No matter how I try to handle the referencing of each other, it ends with this borrowing error.
So, can anyone point out a solution for this situation? Is my app's structure not valid in Rust and I should do it in another way? or there's a trick to this that my inexperienced mind can't find?
Thanks.
DataSourcereally has to keep anything aboutProcessor.DataSource's methods to accept some&Processoras an argument. And if that's boilerplate, thenProcessorstruct could have wrapper methods that call methods ofDataSource, and passing&selfas the argumentmain) and the question in only asking about mutual references.