I have a System, which will connect multiply API downstreams. I would like to use
let mut system = System::new()
to intake config and do the validation, then use
system.init()
to init all connections for the downstreams.
After it connects all downstreams, I would like to make multiple methods to do CRUD to the downstreams.
Here's the playground
struct Conn {
connection: String,
}
impl Conn {
fn fetch(&mut self) -> &str {
self.connection.push_str("data");
&self.connection
}
}
struct System {
downstream: Option<Conn>,
}
impl System {
fn new() -> Self {
System { downstream: None }
}
fn init(&mut self) {
self.downstream = Some(Conn {
connection: String::from("db connection"),
})
}
fn method_a(self) -> String {
let mut conn = self.downstream.unwrap();
String::from(conn.fetch())
}
fn method_b(self) -> String {
let mut conn = self.downstream.unwrap();
String::from(conn.fetch())
}
}
fn main() {
let mut system = System::new();
system.init();
println!("{}", system.method_a());
println!("{}", system.method_b());
}
However, because system.method_a() moved the ownership at unwrap(), so when I call the second method system.method_b() after the first one. Obviously, I will get the error "use of moved value".
I understand how and why the error happens. But I don't know what's the idiomatic way to deal with this situation.
One workaround way is use Conn instead of Option<Conn>, but if don't use Option, then I have to init all downstream connections in System.new() and all downstreams can't have the value of "None".
So my question is how I design a module to fix my purpose?
Thanks in advance.