Skip to content

In Ch20, BufReader is initialized using mutable borrow of TcpStream unnecessarily #3957

@bin-wang

Description

@bin-wang

URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch20-01-single-threaded.html
https://doc.rust-lang.org/book/ch20-02-multithreaded.html

Description of the problem:
In all the code listings in Chapter 20, a BufReader is used to read the HTTP request. The BufReader is initialized using a mutable borrow of TcpStream:

let buf_reader = BufReader::new(&mut stream);

However, it seems the mutable borrow isn't necessary here. The code works just fine when mut is removed. What's the consideration here?

Suggested fix:

Change the initialization to use immutable borrow.

let buf_reader = BufReader::new(&stream);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions