I am writing a TCP client and have a conn
field in my client struct. My client implements two methods new
to instantiate the struct and connect to open a connection to the server and set that as the value of the conn
field
pub struct FistClient {
addr: String,
conn: TcpStream,
}
impl FistClient {
pub fn new(ip: &str, port: &str) -> Self {
FistClient {
addr: String::from(ip) + ":" + &String::from(port),
// conn: <some-defaullt-value>,
}
}
pub fn connect(&mut self, ip: &str, port: &str) {
let res = TcpStream::connect(&self.addr);
match res {
Ok(c) => self.conn = c,
Err(_) => panic!(),
}
}
}
I want to set the conn
field in the new method to some default value. In Go I can do something like conn: nil
but it doesn't work here. I tried Default::default()
too but that trait isn't implemented for TCPStream
how should I set it to a default value?