2

I have a TcpStream that prints out received lines by buffering them in a BufReader.

use std::net::TcpStream;
use std::io::{BufReader, BufRead, Write, BufWriter};

fn main() {
    let stream = TcpStream::connect("irc.freenode.org:6667").unwrap();
    let mut line = String::new();
    let mut reader = BufReader::new(stream);

    // let mut writer = BufWriter::new(stream);  //Issues with moved value  `stream`

    loop {
        let _ = reader.read_line(&mut line);
        println!("{}", line);
        line = String::new();
    }
}

I want to be able to write to the stream as well using a BufWriter but come across issues with use of moved value 'stream'.

How can I read and write on the same TcpStream?

I am aware of a crate that provides this functionality, but want to understand how to do it myself as I am new to Rust.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Hugo Buff
  • 411
  • 5
  • 14

1 Answers1

0

Are you just looking for BufStream ?

dpc.pw
  • 3,462
  • 1
  • 19
  • 24