Rust, take 2

Today's post is more about Rust, the programming language. A followup to last week's post, if you will.

This post needs more Rust knowledge than last week's did to understand fully. As always, you're welcome to read it regardless, but I am not, for example, going to explain what it means to call a value `borrowed'; I am presuming some basic knowledge of Rust.

I've now spent a little while trying to write Rust code. Since what work was ultimately considering using Rust for is fairly large, I went for something smaller as a first attempt. I opted for a rudimentary chat server: connect to it, and every line you send is echoed to all connections.

I have failed. I don't know whether this is because the language is inexplicably crippled, I've missed something, or I'm just too set in my old mental ways.

I was, and am, unable to find any way to do non-blocking I/O in Rust. As a result, I had to try to use multiple threads. I also have been unable to find anything analogous to select(), poll(), or the like. This then means I need two threads per connection, one for writing to it and one for reading from it. (Their end-of-book project, a rudimentary webserver, appears to be carefully chosen in this respect; it never had occasion to try to do simultaneous bidirectional I/O, so I had no examples to work from.)

The approved form of intra-process inter-thread communication is, of course, channels. That's fine. But when I started sending things through them, I got in a snarl. I started out with what I thought was reasonable and I got errors about lifetimes, with the compiler recommending adding lifetime parameters. After a couple of rounds of that, I wound up with "borrowed data escapes outside of function", which has no suggestions for fixes (and is also mysterious to me, because I explicitly clone it before using it in the way that is cited as causing it to escape the function).

I also haven't figured out any way to have one thread writing to a TcpStream and another one reading from it. I keep getting "use of moved value", which I mostly understand, but I can't see what to do about it.

So, I'm stymied...and puzzled. I wouldn't think this would be a difficult thing to do, so I assume I'm just missing something, but I sure ain't seeing what.

Main