1

I have a struct:

pub struct Paradise {
    cstream: TcpStream,
}

with a method:

pub fn write_message(&mut self, code: i32, message: &str) {
    let foo = format!("{} {}\r\n", code, message);
    let _ = self.cstream.write(foo.as_bytes());
}

That works great. It's an FTP server so when I get a new TcpStream from TcpListener::bind I do:

let mut p = Paradise::new(stream);
p.start();

And inside that start method I call:

self.write_message(220, "Welcome to Paradise");

and sure enough, I see that message in the FTP client. So far so good.

But then I do:

let mut br = BufReader::new(&self.cstream);
loop {
    let mut buffer = String::new();
    let _ = br.read_line(&mut buffer);
    println!("{:?}", buffer);
    self.write_message(550, "Testing");
}

And when I get to the next write_message call inside the loop:

cannot borrow *self as mutable because self.cstream is also borrowed as immutable [E0502]

Full code:

https://github.com/andrewarrow/tinted_paradise/blob/169cc5f7025c417814f47a1fb3e3fc78ce4f9516/src/paradise.rs

https://github.com/andrewarrow/tinted_paradise/blob/169cc5f7025c417814f47a1fb3e3fc78ce4f9516/src/starter.rs

How can I change stuff around so I can call write_message inside the loop?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80
  • 1
    There are currently [72 questions for the error message you've specified](http://stackoverflow.com/search?q=%5Brust%5D+is%3Aq+borrow+as+mutable+because+is+also+borrowed+as+immutable+). – Shepmaster Aug 21 '16 at 19:22
  • ^ are you saying I'm using stack overflow incorrectly to learn rust? Why are you so against learning? – Andrew Arrow Aug 21 '16 at 19:27
  • 1
    Haha, I'm certainly not against *learning*. I *am* (obliquely) pointing out that multiple people have spent significant time explaining that error message to ~70 people in ~70 contexts. It would be courteous to read (a good portion of) them in addition to the [normal Rust documentation](https://doc.rust-lang.org/stable/book/) and explain what makes your question different. Note that I answered the question you asked in the title, which seemed unique, but perhaps is actually a duplicate of http://stackoverflow.com/q/35869078/155423 or those linked from there. – Shepmaster Aug 21 '16 at 19:32
  • 3
    Also, I'm pretty sure that "Why are you so against learning?" is some kind of logical fallacy or similar... ^_^ – Shepmaster Aug 21 '16 at 19:35
  • 2
    @AndrewArrow: The purpose of stackoverflow is to get an answer to your question *without* having to ask it. It also aims at *consolidating* knowledge, rather than having it scattered across a dozen half-hearted explanations among so many pages. This means that, in general, if you ask a question that has already been asked, it should be closed. If the answers on the already asked question are insufficient, then it is best to improve *those* rather than leave incomplete answers there. As a corollary, a different question need to clearly indicate *how* it is different; it may not be obvious. – Matthieu M. Aug 21 '16 at 20:22
  • I dunno guys, I think my question was pretty intelligently worded and showed I was really trying. I read the other 72 questions but wanted to ask it my way. What if a college professor responded to an intelligent question from a student with RTFM? Also @Shepmaster what is wrong with using tags in github? https://github.com/andrewarrow/tinted_paradise/tree/so2 so2 is a tag just for this question. – Andrew Arrow Aug 21 '16 at 20:57
  • My rationale for tags -> hashes is in [the edit history](http://stackoverflow.com/posts/39067812/revisions). *I read the other 72 questions* — that is very impressive, kudos! I hope you remembered to upvote Q&A you found useful while reading. It also means that you should be extremely capable of explaining the difference between those questions and this one, making it possible to give a useful answer here. **Now that you've read all the existing answers**, us restating existing answers cannot possibly help you, so helping us understand the difference is the only way you can get a useful answer – Shepmaster Aug 21 '16 at 21:08
  • I believe a professor would start by asking "What have you tried?" I agree your question is well-worded and readable, but *as it is currently written*, it doesn't show any effort to fix the problem. Right now, it's awfully close to "here is my code, here is an error, people on SO will fix it for me". – Shepmaster Aug 21 '16 at 21:09

1 Answers1

4

How do I give a TcpStream to a BufReader but then take it back?

By calling BufReader::into_inner:

impl<R: Read> BufReader<R> {
    fn into_inner(self) -> R
}

Unwraps this BufReader, returning the underlying reader.

Note that any leftover data in the internal buffer is lost.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366