diff --git a/src/ch20-01-single-threaded.md b/src/ch20-01-single-threaded.md index c39a58d..277cab1 100644 --- a/src/ch20-01-single-threaded.md +++ b/src/ch20-01-single-threaded.md @@ -336,9 +336,10 @@ Code in Codeblock 20-3. Dateiname: src/main.rs ```rust,no_run -# use std::io::prelude::*; -# use std::net::TcpListener; -# use std::net::TcpStream; +# use std::{ +# io::{prelude::*, BufReader}, +# net::{TcpListener, TcpStream}, +# }; # # fn main() { # let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); @@ -351,14 +352,16 @@ Code in Codeblock 20-3. # } # fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 1024]; - - stream.read(&mut buffer).unwrap(); + let buf_reader = BufReader::new(&mut stream); + let http_request: Vec<_> = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); let response = "HTTP/1.1 200 OK\r\n\r\n"; - stream.write(response.as_bytes()).unwrap(); - stream.flush().unwrap(); + stream.write_all(response.as_bytes()).unwrap(); } ``` diff --git a/src/ch20-02-multithreaded.md b/src/ch20-02-multithreaded.md index 89f73ff..7443b2f 100644 --- a/src/ch20-02-multithreaded.md +++ b/src/ch20-02-multithreaded.md @@ -484,7 +484,7 @@ annimmt: Dateiname: src/lib.rs -```rust +```rust,noplayground # pub struct ThreadPool; # impl ThreadPool { @@ -608,7 +608,7 @@ pub fn spawn(f: F) -> JoinHandle where F: FnOnce() -> T, F: Send + 'static, - T: Send + 'static + T: Send + 'static, ``` Die Funktion `spawn` gibt einen `JoinHandle` zurück, wobei `T` der Typ ist, @@ -990,12 +990,12 @@ Wenn wir versuchen, diesen Code zu überprüfen, erhalten wir diesen Fehler: $ cargo check Checking hello v0.1.0 (file:///projects/hello) error[E0382]: use of moved value: `receiver` - --> src/lib.rs:27:42 + --> src/lib.rs:26:42 | -22 | let (sender, receiver) = mpsc::channel(); +21 | let (sender, receiver) = mpsc::channel(); | -------- move occurs because `receiver` has type `std::sync::mpsc::Receiver`, which does not implement the `Copy` trait ... -27 | workers.push(Worker::new(id, receiver)); +26 | workers.push(Worker::new(id, receiver)); | ^^^^^^^^ value moved here, in previous iteration of loop For more information about this error, try `rustc --explain E0382`. @@ -1155,7 +1155,7 @@ impl ThreadPool { # # ThreadPool { workers, sender } # } -# + pub fn execute(&self, f: F) where F: FnOnce() + Send + 'static,