Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Proper way to work with TcpStream?
I'm learning Rust having mostly Java background.
fn main() -> std::io::Result<()> {
let mut stream = TcpStream::connect("127.0.0.1:34254")?;
stream.write(&[1])?;
stream.read(&mut [0; 128])?;
Ok(())
}
It's fairly easy to create two threads to read and write in Java. However Rust is stubborn here complaining that reading and writing threads will outlive the creator function. It's obvious they will, but how do I calm down Rust telling that it's the expected behavior? Another problem I didn't meet yet but expect that Rust will tell to use something like Mutex<TcpStream> in threads, but I do not want an exclusive access to TcpStream, because read and write operations need to be executed concurrently. I would prefer to do not use any third party crates for a solution.
1 post - 1 participant
🏷️ rust_feed