I have a thread that reads from and sends to a TcpStream
. I want to be able to send a message to the thread that will then write it to the stream.
I want this pseudocode:
pub fn spawn_thread(mut stream: TcpStream, commands: Receiver<String>) {
std::thread::spawn(move || loop {
// wait for either tcp data or command
if /* got command */ {
handle_write(/* ... */);
} else if /* got tcp data */ {
handle_read(/* ... */);
}
});
}
How would I go about doing this, or am I thinking about it wrong? I'm pretty new to Rust, coming from more of a C/C++ background. I'd think of using a select
statement here. Is there any Rust equivalent?