Skip to content

Commit

Permalink
update original
Browse files Browse the repository at this point in the history
  • Loading branch information
funkill committed Nov 26, 2024
1 parent f27a8fa commit 39d973c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
22 changes: 11 additions & 11 deletions rust-cookbook/src/algorithms/sorting/sort_struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ 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
}
}
}
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)
Expand All @@ -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
Expand All @@ -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),
]);
}
Expand Down
6 changes: 3 additions & 3 deletions rust-cookbook/src/encoding/csv/delimiter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Record>() {
Expand Down
9 changes: 5 additions & 4 deletions rust-cookbook/src/encoding/string/percent-encode.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions rust-cookbook/src/web/clients/download/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion rust-cookbook/src/web/clients/download/partial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down

0 comments on commit 39d973c

Please sign in to comment.