async-book
async-book copied to clipboard
Examples in book mostly not work
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.
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.
- 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 atests
directory andcargo new
doesn't generate this block by default so it was a bit unclear if a test insrc/bin/main.rs
would even run. - The response formatting changed to
let response = format!("{}{}", status_line, contents);
to remove theContent-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? - Relating to ^, the
assert!(stream.write_data.starts_with(expected_response.as_bytes()));
test panics if thehandle_connection
function is still returning theContent-Length
header, but is quite difficult to to debug, as it's just checking abool
response fromstarts_with
comparing bytes, rather than doing a string comparison withassert_eq!
or offering any debugging tips to stringify the stream data.