-
Notifications
You must be signed in to change notification settings - Fork 342
Open
Labels
question/feedbackA question or user feedbackA question or user feedback
Description
Echo server example in TcpListener seems confusing to a new-comer.
while let Some(stream) = incoming.next().await {
let stream = stream?; -- Immutable binding
let (reader, writer) = &mut (&stream, &stream); -- This take mutable reference of immutable references
io::copy(reader, writer).await?; -- but io requires mutable access.
}
It's not clear how io::copy
can use reader and writer which are basically &mut &TcpStream
Metadata
Metadata
Assignees
Labels
question/feedbackA question or user feedbackA question or user feedback
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
ghost commentedon Nov 7, 2019
This change would solve the problem: #365
Then one could write:
gyscos commentedon Jan 16, 2020
It can currently be written:
This is because we have something like
impl Read for &TcpStream
(and same thing forWrite
), so&stream
is bothRead
and aWrite
.io::copy
wants mutable reference to both, so&mut &stream
is now a&mut impl Read
.I think another confusing part is how this example only processes one client at a time, which is a bit sad for an async framework. As is, if a client connects and keep the connection open, no other client can connect.
Spawning a task would be a better idea: