I am writing a client for a TCP server
use std::net::TcpStream;
use std::io::{Read, Write, Error};
pub struct Client {
addr: String,
conn: Option<TcpStream>,
}
impl Client {
pub fn connect(&mut self) {
let res = TcpStream::connect(&self.addr);
match res {
Ok(c) => {
self.conn = Some(c);
},
Err(_) => panic!(),
}
}
pub fn version(self) -> Result<[u8; 8], Error> {
let mut ver: [u8; 8] = [0;8];
let command_string = b"VERSION\r\n";
let res = self.conn.unwrap().write(&command_string[0..]);
match res {
Ok(_) => self.conn.unwrap().read(&mut ver[..]),
Err(e) => panic!(e)
};
Ok(ver)
}
}
The version method returns an error
error[E0382]: use of moved value: `self.conn`
when I try to reuse the reference. I thought about using #[derive(Copy, Clone)]
but since these traits are not implemented for TCPStream I am not sure what to do. What would be the best way to achieve reading and writing to a server in a method (btw there will be many such methods like version which will both read and write to the server)
I am still new to rust and trying to wrap my head around ownership concepts so I apologise if this is a stupid question but since I want to discuss various approaches (if indeed there are multiple approaches to solving this problem) to reuse un-Clonable/Copyable fields I thought this question was worth asking.