I am trying to add an item to a Vec<T>, call a function and await it. I am still wrapping my head around the ownership and borrow rules. I understand the below won't work:
pub async fn begin_session(&mut self, stream: TcpStream) {
let session = Session::new(stream);
self.sessions.push(session);
session.start_receiver().await; // obviously, won't work
}
The solution that I came up with below doesn't seem ideal (or thread-safe) to me:
pub async fn begin_session(&mut self, stream: TcpStream) {
let session = Session::new(stream);
self.sessions.push(session);
let session = self.sessions.last().unwrap();
session.start_receiver().await; // That works.
}
Is there a function in Vec<T> that does a push(T) and also returns T?