From 209d89b20533a7e5fcc0ee7db35586010909f490 Mon Sep 17 00:00:00 2001 From: funkill2 Date: Fri, 29 Nov 2024 04:00:11 +0300 Subject: [PATCH] update original --- rust-cookbook/Cargo.toml | 4 +- rust-cookbook/src/about.md | 2 +- .../src/compression/tar/tar-compress.md | 26 +++++++++- .../src/compression/tar/tar-strip-prefix.md | 4 +- rust-cookbook/src/data_structures/bitfield.md | 2 +- rust-cookbook/src/development_tools.md | 47 ++++++++++++++----- .../src/development_tools/debugging.md | 40 ++++++++-------- rust-cookbook/src/encoding/string/hex.md | 2 +- rust-cookbook/src/links.md | 6 +-- rust-cookbook/src/net/server/listen-unused.md | 10 ++-- rust-cookbook/src/science.md | 34 +++++++++++++- rust-cookbook/src/web.md | 2 +- rust-cookbook/src/web/clients.md | 2 +- .../src/web/clients/download/basic.md | 2 +- rust-cookbook/src/web/clients/requests/get.md | 4 +- 15 files changed, 132 insertions(+), 55 deletions(-) diff --git a/rust-cookbook/Cargo.toml b/rust-cookbook/Cargo.toml index a37b2f8..e08f150 100755 --- a/rust-cookbook/Cargo.toml +++ b/rust-cookbook/Cargo.toml @@ -63,9 +63,9 @@ walkdir = "2.0" syslog = "5.0" [build-dependencies] -skeptic = { git = 'https://github.com/andygauge/rust-skeptic'} +skeptic = "0.13" walkdir = "2.0" [dev-dependencies] -skeptic = { git = 'https://github.com/andygauge/rust-skeptic'} +skeptic = "0.13" walkdir = "2.0" diff --git a/rust-cookbook/src/about.md b/rust-cookbook/src/about.md index e1d0b70..7de8904 100644 --- a/rust-cookbook/src/about.md +++ b/rust-cookbook/src/about.md @@ -144,7 +144,7 @@ work. For the sake of readability error handling boilerplate is hidden by default like below. In order to read full contents click on the -"expand" () button located in the top +"Show hidden lines" () button located in the top right corner of the snippet. ```rust,edition2018 diff --git a/rust-cookbook/src/compression/tar/tar-compress.md b/rust-cookbook/src/compression/tar/tar-compress.md index 25735a6..d245434 100644 --- a/rust-cookbook/src/compression/tar/tar-compress.md +++ b/rust-cookbook/src/compression/tar/tar-compress.md @@ -6,7 +6,7 @@ Compress `/var/log` directory into `archive.tar.gz`. Creates a [`File`] wrapped in [`GzEncoder`] and [`tar::Builder`].
Adds contents of `/var/log` directory recursively into the archive -under `backup/logs`path with [`Builder::append_dir_all`]. +under `backup/logs` path with [`Builder::append_dir_all`]. [`GzEncoder`] is responsible for transparently compressing the data prior to writing it into `archive.tar.gz`. @@ -21,11 +21,35 @@ fn main() -> Result<(), std::io::Error> { let enc = GzEncoder::new(tar_gz, Compression::default()); let mut tar = tar::Builder::new(enc); tar.append_dir_all("backup/logs", "/var/log")?; + tar.finish()?; Ok(()) } ``` +To add the contents without renaming them, an empty string can be used as the first argument of [`Builder::append_dir_all`]: + +```rust,edition2018,no_run + +use std::fs::File; +use flate2::Compression; +use flate2::write::GzEncoder; + +fn main() -> Result<(), std::io::Error> { + let tar_gz = File::create("archive.tar.gz")?; + let enc = GzEncoder::new(tar_gz, Compression::default()); + let mut tar = tar::Builder::new(enc); + tar.append_dir_all("", "/var/log")?; + tar.finish()?; + Ok(()) +} +``` + +The default behavior of [`tar::Builder`] differs from the GNU `tar` utility's defaults [tar(1)], +notably [`tar::Builder::follow_symlinks(true)`] is the equivalent of `tar --dereference`. + +[tar(1)]: https://man7.org/linux/man-pages/man1/tar.1.html [`Builder::append_dir_all`]: https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html [`GzEncoder`]: https://docs.rs/flate2/*/flate2/write/struct.GzEncoder.html [`tar::Builder`]: https://docs.rs/tar/*/tar/struct.Builder.html +[`tar::Builder::follow_symlinks(true)`]: https://docs.rs/tar/latest/tar/struct.Builder.html#method.follow_symlinks diff --git a/rust-cookbook/src/compression/tar/tar-strip-prefix.md b/rust-cookbook/src/compression/tar/tar-strip-prefix.md index a770871..dd2f0ab 100644 --- a/rust-cookbook/src/compression/tar/tar-strip-prefix.md +++ b/rust-cookbook/src/compression/tar/tar-strip-prefix.md @@ -20,7 +20,7 @@ use tar::Archive; # } # } -fn main() -> Result<()> { +fn main() -> Result<(), std::io::Error> { let file = File::open("archive.tar.gz")?; let mut archive = Archive::new(GzDecoder::new(file)); let prefix = "bundle/logs"; @@ -29,7 +29,7 @@ fn main() -> Result<()> { archive .entries()? .filter_map(|e| e.ok()) - .map(|mut entry| -> Result { + .map(|mut entry| -> Result> { let path = entry.path()?.strip_prefix(prefix)?.to_owned(); entry.unpack(&path)?; Ok(path) diff --git a/rust-cookbook/src/data_structures/bitfield.md b/rust-cookbook/src/data_structures/bitfield.md index ff45ddf..2ba0e49 100644 --- a/rust-cookbook/src/data_structures/bitfield.md +++ b/rust-cookbook/src/data_structures/bitfield.md @@ -1,4 +1,4 @@ -# Custom +# Bitfield {{#include bitfield/bitfield.md}} diff --git a/rust-cookbook/src/development_tools.md b/rust-cookbook/src/development_tools.md index 94b167a..eb41b1e 100644 --- a/rust-cookbook/src/development_tools.md +++ b/rust-cookbook/src/development_tools.md @@ -1,31 +1,52 @@ # Development Tools -{{#include development_tools/debugging.md}} +## Debugging + +| Recipe | Crates | Categories | +| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | --------------------------------------- | +| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] | +| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] | +| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] | +| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] | + +[ex-log-debug]: development_tools/debugging/log.html#log-a-debug-message-to-the-console +[ex-log-error]: development_tools/debugging/log.html#log-an-error-message-to-the-console +[ex-log-stdout]: development_tools/debugging/log.html#log-to-stdout-instead-of-stderr +[ex-log-custom-logger]: development_tools/debugging/log.html#log-messages-with-a-custom-logger +[ex-log-syslog]: development_tools/debugging/log.html#log-to-the-unix-syslog +[ex-log-mod]: development_tools/debugging/config_log.html#enable-log-levels-per-module +[ex-log-env-variable]: development_tools/debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging +[ex-log-timestamp]: development_tools/debugging/config_log.html#include-timestamp-in-log-messages +[ex-log-custom]: development_tools/debugging/config_log.html#log-messages-to-a-custom-location ## Versioning -| Recipe | Crates | Categories | -|--------|--------|------------| -| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | -| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | -| [Check if given version is pre-release][ex-semver-prerelease] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | -| [Find the latest version satisfying given range][ex-semver-latest] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | -| [Check external command version for compatibility][ex-semver-command] | [![semver-badge]][semver] | [![cat-text-processing-badge]][cat-text-processing] [![cat-os-badge]][cat-os] +| Recipe | Crates | Categories | +| --------------------------------------------------------------------- | ------------------------- | ----------------------------------------------------------------------------- | +| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | +| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | +| [Check if given version is pre-release][ex-semver-prerelease] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | +| [Find the latest version satisfying given range][ex-semver-latest] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] | +| [Check external command version for compatibility][ex-semver-command] | [![semver-badge]][semver] | [![cat-text-processing-badge]][cat-text-processing] [![cat-os-badge]][cat-os] | ## Build Time -| Recipe | Crates | Categories | -|--------|--------|------------| -| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] | +| Recipe | Crates | Categories | +| -------------------------------------------------------------------------------- | ----------------- | ------------------------------------------------------- | +| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] | | [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] | -| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] | +| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] | [ex-semver-increment]: development_tools/versioning.html#parse-and-increment-a-version-string [ex-semver-complex]: development_tools/versioning.html#parse-a-complex-version-string [ex-semver-prerelease]: development_tools/versioning.html#check-if-given-version-is-pre-release [ex-semver-latest]: development_tools/versioning.html#find-the-latest-version-satisfying-given-range [ex-semver-command]: development_tools/versioning.html#check-external-command-version-for-compatibility - [ex-cc-static-bundled]: development_tools/build_tools.html#compile-and-link-statically-to-a-bundled-c-library [ex-cc-static-bundled-cpp]: development_tools/build_tools.html#compile-and-link-statically-to-a-bundled-c-library-1 [ex-cc-custom-defines]: development_tools/build_tools.html#compile-a-c-library-while-setting-custom-defines diff --git a/rust-cookbook/src/development_tools/debugging.md b/rust-cookbook/src/development_tools/debugging.md index 6786f8b..c921575 100644 --- a/rust-cookbook/src/development_tools/debugging.md +++ b/rust-cookbook/src/development_tools/debugging.md @@ -1,25 +1,25 @@ ## Debugging -| Recipe | Crates | Categories | -|--------|--------|------------| -| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | -| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | -| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | -| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] | -| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] | -| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | -| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | -| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] | -| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] | +| Recipe | Crates | Categories | +| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | --------------------------------------- | +| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] | +| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] | +| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] | +| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] | +| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] | -[ex-log-debug]: development_tools/debugging/log.html#log-a-debug-message-to-the-console -[ex-log-error]: development_tools/debugging/log.html#log-an-error-message-to-the-console -[ex-log-stdout]: development_tools/debugging/log.html#log-to-stdout-instead-of-stderr -[ex-log-custom-logger]: development_tools/debugging/log.html#log-messages-with-a-custom-logger -[ex-log-syslog]: development_tools/debugging/log.html#log-to-the-unix-syslog -[ex-log-mod]: development_tools/debugging/config_log.html#enable-log-levels-per-module -[ex-log-env-variable]: development_tools/debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging -[ex-log-timestamp]: development_tools/debugging/config_log.html#include-timestamp-in-log-messages -[ex-log-custom]: development_tools/debugging/config_log.html#log-messages-to-a-custom-location +[ex-log-debug]: debugging/log.html#log-a-debug-message-to-the-console +[ex-log-error]: debugging/log.html#log-an-error-message-to-the-console +[ex-log-stdout]: debugging/log.html#log-to-stdout-instead-of-stderr +[ex-log-custom-logger]: debugging/log.html#log-messages-with-a-custom-logger +[ex-log-syslog]: debugging/log.html#log-to-the-unix-syslog +[ex-log-mod]: debugging/config_log.html#enable-log-levels-per-module +[ex-log-env-variable]: debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging +[ex-log-timestamp]: debugging/config_log.html#include-timestamp-in-log-messages +[ex-log-custom]: debugging/config_log.html#log-messages-to-a-custom-location {{#include ../links.md}} diff --git a/rust-cookbook/src/encoding/string/hex.md b/rust-cookbook/src/encoding/string/hex.md index 3cd2914..be9ce95 100644 --- a/rust-cookbook/src/encoding/string/hex.md +++ b/rust-cookbook/src/encoding/string/hex.md @@ -9,7 +9,7 @@ representation of the data. Similarly, a `HEXUPPER::decode` method is provided which takes a `&[u8]` and returns a `Vec` if the input data is successfully decoded. -The example below coverts `&[u8]` data to hexadecimal equivalent. Compares this +The example below converts `&[u8]` data to hexadecimal equivalent. Compares this value to the expected value. ```rust,edition2018 diff --git a/rust-cookbook/src/links.md b/rust-cookbook/src/links.md index 072ae19..9458590 100644 --- a/rust-cookbook/src/links.md +++ b/rust-cookbook/src/links.md @@ -105,7 +105,7 @@ Keep lines sorted. [postgres]: https://docs.rs/postgres/0.15.2/postgres/ [rand-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand [rand]: https://docs.rs/rand/ -[rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand_distr +[rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand_distr.svg?label=rand_distr [rand_distr]: https://docs.rs/rand_distr/ [rayon-badge]: https://badge-cache.kominick.com/crates/v/rayon.svg?label=rayon [rayon]: https://docs.rs/rayon/ @@ -133,8 +133,8 @@ Keep lines sorted. [syslog]: https://docs.rs/syslog/ [tar-badge]: https://badge-cache.kominick.com/crates/v/tar.svg?label=tar [tar]: https://docs.rs/tar/ -[tempdir-badge]: https://badge-cache.kominick.com/crates/v/tempdir.svg?label=tempdir -[tempdir]: https://docs.rs/tempdir/ +[tempfile-badge]: https://badge-cache.kominick.com/crates/v/tempfile.svg?label=tempfile +[tempfile]: https://docs.rs/tempfile/ [threadpool-badge]: https://badge-cache.kominick.com/crates/v/threadpool.svg?label=threadpool [threadpool]: https://docs.rs/threadpool/ [toml-badge]: https://badge-cache.kominick.com/crates/v/toml.svg?label=toml diff --git a/rust-cookbook/src/net/server/listen-unused.md b/rust-cookbook/src/net/server/listen-unused.md index 4677cb8..cda04d9 100644 --- a/rust-cookbook/src/net/server/listen-unused.md +++ b/rust-cookbook/src/net/server/listen-unused.md @@ -3,17 +3,15 @@ [![std-badge]][std] [![cat-net-badge]][cat-net] In this example, the port is displayed on the console, and the program will -listen until a request is made. `SocketAddrV4` assigns a random port when -setting port to 0. +listen until a request is made. `TcpListener::bind` uses a random port +allocated by the OS when requested to bind to port 0. ```rust,edition2018,no_run -use std::net::{SocketAddrV4, Ipv4Addr, TcpListener}; +use std::net::TcpListener; use std::io::{Read, Error}; fn main() -> Result<(), Error> { - let loopback = Ipv4Addr::new(127, 0, 0, 1); - let socket = SocketAddrV4::new(loopback, 0); - let listener = TcpListener::bind(socket)?; + let listener = TcpListener::bind("localhost:0")?; let port = listener.local_addr()?; println!("Listening on {}, access this port to end the program", port); let (mut tcp_stream, addr) = listener.accept()?; //block until requested diff --git a/rust-cookbook/src/science.md b/rust-cookbook/src/science.md index f698ba6..5694bf6 100644 --- a/rust-cookbook/src/science.md +++ b/rust-cookbook/src/science.md @@ -1,5 +1,37 @@ # Science -{{#include science/mathematics.md}} +## science/mathematics + +| Recipe | Crates | Categories | +| ------------------------------------------------------------------------------ | ----------------------------- | ----------------------------------- | +| [Vector Norm][vector-norm] | [![ndarray-badge]][ndarray] | [![cat-science-badge]][cat-science] | +| [Adding matrices][add-matrices] | [![ndarray-badge]][ndarray] | [![cat-science-badge]][cat-science] | +| [Multiplying matrices][multiply-matrices] | [![ndarray-badge]][ndarray] | [![cat-science-badge]][cat-science] | +| [Multiply a scalar with a vector with a matrix][multiply-scalar-vector-matrix] | [![ndarray-badge]][ndarray] | [![cat-science-badge]][cat-science] | +| [Invert matrix][invert-matrix] | [![nalgebra-badge]][nalgebra] | [![cat-science-badge]][cat-science] | +| [Calculating the side length of a triangle][side-length] | [![std-badge]][std] | [![cat-science-badge]][cat-science] | +| [Verifying tan is equal to sin divided by cos][tan-sin-cos] | [![std-badge]][std] | [![cat-science-badge]][cat-science] | +| [Distance between two points on the Earth][latitude-longitude] | [![std-badge]][std] | [![cat-science-badge]][cat-science] | +| [Creating complex numbers][create-complex] | [![num-badge]][num] | [![cat-science-badge]][cat-science] | +| [Adding complex numbers][add-complex] | [![num-badge]][num] | [![cat-science-badge]][cat-science] | +| [Mathematical functions on complex numbers][mathematical-functions] | [![num-badge]][num] | [![cat-science-badge]][cat-science] | +| [Measures of central tendency][ex-central-tendency] | [![std-badge]][std] | [![cat-science-badge]][cat-science] | +| [Computing standard deviation][ex-standard-deviation] | [![std-badge]][std] | [![cat-science-badge]][cat-science] | +| [Big integers][big-integers] | [![num-badge]][num] | [![cat-science-badge]][cat-science] | + +[vector-norm]: science/mathematics/linear_algebra.html#vector-norm +[add-matrices]: science/mathematics/linear_algebra.html#adding-matrices +[multiply-matrices]: science/mathematics/linear_algebra.html#multiplying-matrices +[multiply-scalar-vector-matrix]: science/mathematics/linear_algebra.html#multiply-a-scalar-with-a-vector-with-a-matrix +[invert-matrix]: science/mathematics/linear_algebra.html#invert-matrix +[side-length]: science/mathematics/trigonometry.html#calculating-the-side-length-of-a-triangle +[tan-sin-cos]: science/mathematics/trigonometry.html#verifying-tan-is-equal-to-sin-divided-by-cos +[latitude-longitude]: science/mathematics/trigonometry.html#distance-between-two-points-on-the-earth +[create-complex]: science/mathematics/complex_numbers.html#creating-complex-numbers +[add-complex]: science/mathematics/complex_numbers.html#adding-complex-numbers +[mathematical-functions]: science/mathematics/complex_numbers.html#mathematical-functions +[ex-central-tendency]: science/mathematics/statistics.html#measures-of-central-tendency +[ex-standard-deviation]: science/mathematics/statistics.html#standard-deviation +[big-integers]: science/mathematics/miscellaneous.html#big-integers {{#include links.md}} diff --git a/rust-cookbook/src/web.md b/rust-cookbook/src/web.md index 95b32dd..73403a4 100644 --- a/rust-cookbook/src/web.md +++ b/rust-cookbook/src/web.md @@ -36,7 +36,7 @@ | [Check if an API resource exists][ex-rest-head] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | | [Create and delete Gist with GitHub API][ex-rest-post] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] | | [Consume a paginated RESTful API][ex-paginated-api] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] | -| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] | +| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempfile-badge]][tempfile] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] | | [Make a partial download with HTTP range headers][ex-progress-with-range] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | | [POST a file to paste-rs][ex-file-post] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | diff --git a/rust-cookbook/src/web/clients.md b/rust-cookbook/src/web/clients.md index ab91ac9..01da967 100644 --- a/rust-cookbook/src/web/clients.md +++ b/rust-cookbook/src/web/clients.md @@ -8,7 +8,7 @@ | [Check if an API resource exists][ex-rest-head] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | | [Create and delete Gist with GitHub API][ex-rest-post] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] | | [Consume a paginated RESTful API][ex-paginated-api] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] | -| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] | +| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempfile-badge]][tempfile] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] | | [Make a partial download with HTTP range headers][ex-progress-with-range] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | | [POST a file to paste-rs][ex-file-post] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | diff --git a/rust-cookbook/src/web/clients/download/basic.md b/rust-cookbook/src/web/clients/download/basic.md index d246348..3efae14 100755 --- a/rust-cookbook/src/web/clients/download/basic.md +++ b/rust-cookbook/src/web/clients/download/basic.md @@ -1,6 +1,6 @@ ## Download a file to a temporary directory -[![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] +[![reqwest-badge]][reqwest] [![tempfile-badge]][tempfile] [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] Creates a temporary directory with [`tempfile::Builder`] and downloads a file over HTTP using [`reqwest::get`] asynchronously. diff --git a/rust-cookbook/src/web/clients/requests/get.md b/rust-cookbook/src/web/clients/requests/get.md index 578541d..851aca9 100644 --- a/rust-cookbook/src/web/clients/requests/get.md +++ b/rust-cookbook/src/web/clients/requests/get.md @@ -37,6 +37,8 @@ fn main() -> Result<()> { A similar approach can be used by including the [`tokio`] executor to make the main function asynchronous, retrieving the same information. +Make sure to add tokio = {version = "1.21.2", features = ["full"]} to +your cargo.toml file. In this example, [`tokio::main`] handles all the heavy executor setup and allows sequential code implemented without blocking until `.await`. @@ -55,7 +57,7 @@ error_chain! { } #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> Result<(), Box> { let res = reqwest::get("http://httpbin.org/get").await?; println!("Status: {}", res.status()); println!("Headers:\n{:#?}", res.headers());