rust-lang / book

The Rust Programming Language

Home Page:https://doc.rust-lang.org/book/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chapter 20. Listing 20-25 is inconsistent with the rest of the code in the chapter

skoocda opened this issue · comments

The current web-server configuration, as of listing 20-24 uses the following TCP stream handling logic:

fn handle_connection(mut stream: TcpStream) {
    let buf_reader = BufReader::new(&mut stream);
    let request_line = buf_reader.lines().next().unwrap().unwrap();

    let (status_line, filename) = match &request_line[..] {

This changes without comment in the next listing, 20-25, which uses the following logic:

fn handle_connection(mut stream: TcpStream) {
    let mut buffer = [0; 1024];
    stream.read(&mut buffer).unwrap();

    let get = b"GET / HTTP/1.1\r\n";
    let sleep = b"GET /sleep HTTP/1.1\r\n";

    let (status_line, filename) = if buffer.starts_with(get) {

This logic changes silently between 20-24 and 20-25, although it is not evident until the final code is displayed on https://doc.rust-lang.org/book/ch20-03-graceful-shutdown-and-cleanup.html

This leads to a final codebase that is inconsistent with the reader's code developed via the step-by-step process.

  • Suggested fix:

Keep the prior style for consistency (e.g. the buf_reader with match case), or provide reasoning as to why the buffer style improves on the design. The latter case will require additional writing.

Indeed! See #3857 for an open PR that should resolve this – thanks for filing it!