diff --git a/rust-cookbook/src/algorithms/sorting/sort_struct.md b/rust-cookbook/src/algorithms/sorting/sort_struct.md index ba73298..cd43070 100644 --- a/rust-cookbook/src/algorithms/sorting/sort_struct.md +++ b/rust-cookbook/src/algorithms/sorting/sort_struct.md @@ -15,9 +15,9 @@ struct Person { } impl Person { - pub fn new(name: String, age: u32) -> Self { + pub fn new(name: &str, age: u32) -> Self { Person { - name, + name: name.to_string(), age } } @@ -25,9 +25,9 @@ impl Person { fn main() { let mut people = vec![ - Person::new("Zoe".to_string(), 25), - Person::new("Al".to_string(), 60), - Person::new("John".to_string(), 1), + Person::new("Zoe", 25), + Person::new("Al", 60), + Person::new("John", 1), ]; // Sort people by derived natural order (Name and age) @@ -36,9 +36,9 @@ fn main() { assert_eq!( people, vec![ - Person::new("Al".to_string(), 60), - Person::new("John".to_string(), 1), - Person::new("Zoe".to_string(), 25), + Person::new("Al", 60), + Person::new("John", 1), + Person::new("Zoe", 25), ]); // Sort people by age @@ -47,9 +47,9 @@ fn main() { assert_eq!( people, vec![ - Person::new("Al".to_string(), 60), - Person::new("Zoe".to_string(), 25), - Person::new("John".to_string(), 1), + Person::new("Al", 60), + Person::new("Zoe", 25), + Person::new("John", 1), ]); } diff --git a/rust-cookbook/src/encoding/csv/delimiter.md b/rust-cookbook/src/encoding/csv/delimiter.md index 50d2128..cf719aa 100644 --- a/rust-cookbook/src/encoding/csv/delimiter.md +++ b/rust-cookbook/src/encoding/csv/delimiter.md @@ -18,9 +18,9 @@ struct Record { use csv::ReaderBuilder; fn main() -> Result<(), Error> { - let data = "name\tplace\tid - Mark\tMelbourne\t46 - Ashley\tZurich\t92"; + let data = "name\tplace\tid\n\ + Mark\tMelbourne\t46\n\ + Ashley\tZurich\t92"; let mut reader = ReaderBuilder::new().delimiter(b'\t').from_reader(data.as_bytes()); for result in reader.deserialize::() { diff --git a/rust-cookbook/src/encoding/string/percent-encode.md b/rust-cookbook/src/encoding/string/percent-encode.md index 1e47602..f617d5b 100644 --- a/rust-cookbook/src/encoding/string/percent-encode.md +++ b/rust-cookbook/src/encoding/string/percent-encode.md @@ -2,9 +2,9 @@ [![percent-encoding-badge]][percent-encoding] [![cat-encoding-badge]][cat-encoding] -Encode an input string with [percent-encoding] using the [`utf8_percent_encode`] -function from the `percent-encoding` crate. Then decode using the [`percent_decode`] -function. +Encode an input string with [percent-encoding][percent-encoding-wiki] using the +[`utf8_percent_encode`] function from the `percent-encoding` crate. Then decode +using the [`percent_decode`] function. ```rust,edition2018 use percent_encoding::{utf8_percent_encode, percent_decode, AsciiSet, CONTROLS}; @@ -38,4 +38,5 @@ a `String`. [`percent_decode`]: https://docs.rs/percent-encoding/*/percent_encoding/fn.percent_decode.html [`utf8_percent_encode`]: https://docs.rs/percent-encoding/*/percent_encoding/fn.utf8_percent_encode.html -[percent-encoding]: https://en.wikipedia.org/wiki/Percent-encoding +[percent-encoding]: https://docs.rs/percent-encoding/ +[percent-encoding-wiki]: https://en.wikipedia.org/wiki/Percent-encoding diff --git a/rust-cookbook/src/science/mathematics/trigonometry/side-length.md b/rust-cookbook/src/science/mathematics/trigonometry/side-length.md index 951329c..02b0cb1 100644 --- a/rust-cookbook/src/science/mathematics/trigonometry/side-length.md +++ b/rust-cookbook/src/science/mathematics/trigonometry/side-length.md @@ -2,11 +2,11 @@ [![std-badge]][std] [![cat-science-badge]][cat-science] -Calculates the length of the hypotenuse of a right-angle triangle with an angle of 2 radians and opposite side length of 80. +Calculates the length of the hypotenuse of a right-angle triangle with an angle of 1 radian and opposite side length of 80. ```rust,edition2018 fn main() { - let angle: f64 = 2.0; + let angle: f64 = 1.0; let side_length = 80.0; let hypotenuse = side_length / angle.sin(); diff --git a/rust-cookbook/src/web/clients/download/basic.md b/rust-cookbook/src/web/clients/download/basic.md index 63168ca..d246348 100755 --- a/rust-cookbook/src/web/clients/download/basic.md +++ b/rust-cookbook/src/web/clients/download/basic.md @@ -6,12 +6,12 @@ Creates a temporary directory with [`tempfile::Builder`] and downloads a file over HTTP using [`reqwest::get`] asynchronously. Creates a target [`File`] with name obtained from [`Response::url`] within -[`tempdir()`] and copies downloaded data into it with [`io::copy`]. +[`tempdir()`] and writes downloaded data into it with [`Writer::write_all`]. The temporary directory is automatically removed on program exit. ```rust,edition2018,no_run use error_chain::error_chain; -use std::io::copy; +use std::io::Write; use std::fs::File; use tempfile::Builder; @@ -41,15 +41,15 @@ async fn main() -> Result<()> { println!("will be located under: '{:?}'", fname); File::create(fname)? }; - let content = response.text().await?; - copy(&mut content.as_bytes(), &mut dest)?; + let content = response.bytes().await?; + dest.write_all(&content)?; Ok(()) } ``` [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html -[`io::copy`]: https://doc.rust-lang.org/std/io/fn.copy.html [`reqwest::get`]: https://docs.rs/reqwest/*/reqwest/fn.get.html [`Response::url`]: https://docs.rs/reqwest/*/reqwest/struct.Response.html#method.url [`tempfile::Builder`]: https://docs.rs/tempfile/*/tempfile/struct.Builder.html -[`tempdir()`]: https://docs.rs/tempfile/3.1.0/tempfile/struct.Builder.html#method.tempdir +[`tempdir()`]: https://docs.rs/tempfile/*/tempfile/struct.Builder.html#method.tempdir +[`Writer::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all diff --git a/rust-cookbook/src/web/clients/download/partial.md b/rust-cookbook/src/web/clients/download/partial.md index d288573..9b00bc0 100644 --- a/rust-cookbook/src/web/clients/download/partial.md +++ b/rust-cookbook/src/web/clients/download/partial.md @@ -5,7 +5,7 @@ Uses [`reqwest::blocking::Client::head`] to get the [Content-Length] of the response. The code then uses [`reqwest::blocking::Client::get`] to download the content in -chunks of 10240 bytes, while printing progress messages. This exmple uses the synchronous +chunks of 10240 bytes, while printing progress messages. This example uses the synchronous reqwest module. The [Range] header specifies the chunk size and position. The Range header is defined in [RFC7233][HTTP Range RFC7233].