async-book icon indicating copy to clipboard operation
async-book copied to clipboard

Examples in book mostly not work

Open rerime opened this issue 5 years ago • 1 comments

Expamples provided in book is a part of working solution. For noobies like me it is not clear how to get working example. I guess every example should be covered by CI/CD test.

rerime avatar Dec 03 '19 12:12 rerime

I was able to get through most of the exercises with enough attention to detail, but one specific issue I noticed was in the final exercise to test the async HTTP server, https://github.com/rust-lang/async-book/blob/master/examples/09_05_final_tcp_server/src/main.rs

Copy-pasting the full file from the source does work, but there are a few hiccups that are unclear when working through the book incrementally.

  1. The #[cfg(test)] mod tests { lines are excluded from the snippet, and it's not mentioned explicitly in which file the tests should be written - the Rust Book mentions that tests for binary crates can't be written externally in a tests directory and cargo new doesn't generate this block by default so it was a bit unclear if a test in src/bin/main.rs would even run.
  2. The response formatting changed to let response = format!("{}{}", status_line, contents); to remove the Content-Length header, but this is never mentioned explicitly, and is easy to miss. I'm assuming this is because the full length of a response may not be known/set by a typical streamed response implementation?
  3. Relating to ^, the assert!(stream.write_data.starts_with(expected_response.as_bytes())); test panics if the handle_connection function is still returning the Content-Length header, but is quite difficult to to debug, as it's just checking a bool response from starts_with comparing bytes, rather than doing a string comparison with assert_eq! or offering any debugging tips to stringify the stream data.

mikemorris avatar Aug 02 '21 18:08 mikemorris