I want to read a line from a TCPStream
, write another line to it, and then repeat. The issue is that BufReader::new
takes ownership of my TCPStream
variable:
let stream = ...; // TCPStream
let reader = BufReader::new(stream); // moved its value
// can't use stream here anymore
What is a simple solution to this?
You have several possibilities:
BufReader
BufReader
usingget_mut
BufReader
entirely and recover the stream usinginto_inner
I would personally advise not using a
BufReader
unless you really need to bufferize the input; for a single line it does not seem worth it.Otherwise, if you are done with the buffering, you can recover the underlying stream and if you are not you can temporarily buffer it.
Caution: Do be aware that the
BufReader
bufferizes reads, so when you borrow/recover the inner stream you short-circuit the buffered data; for reading it's an issue, in your case (writing) it should be fine.Solution: use references.
Explanation
If we take a closer look at
BufReader::new
, we see that it takes an argumentinner
of typeR
, whereR
is just any type that implementsRead
:We then take a look at
Read
and see this implementation:So we can just pass a reference to the
new
function, like so:We will do the same for
BufReader
andWrite
and will see that indeed there is this implementation:So we can, again, use an immutable reference to create the
BufWriter
.