Skip to content

Commit 8aec451

Browse files
committed
merge master
1 parent db06504 commit 8aec451

22 files changed

+362
-834
lines changed

src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[Table of Contents](intro.md)
44
[About](about.md)
5-
65
- [Algorithms](algorithms.md)
76
- [Generate Random Values](algorithms/randomness.md)
87
- [Command Line](cli.md)
@@ -46,6 +45,7 @@
4645
- [Web Programming](web.md)
4746
- [Scraping Web Pages](web/scraping.md)
4847
- [Uniform Resource Location](web/url.md)
48+
- [Media Types](web/mime.md)
4949
- [Clients](web/clients.md)
5050
- [Making Requests](web/clients/requests.md)
5151
- [Using APIs](web/clients/apis.md)

src/concurrency/parallel/rayon-thumbnails.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate image;
1616
extern crate rayon;
1717
1818
use std::path::Path;
19-
use std::fs::{create_dir_all, File};
19+
use std::fs::create_dir_all;
2020
2121
# use error_chain::ChainedError;
2222
use glob::{glob_with, MatchOptions};
@@ -71,10 +71,10 @@ where
7171
PB: AsRef<Path>,
7272
{
7373
let img = image::open(original.as_ref())?;
74-
let fout = &mut File::create(thumb_dir.as_ref().join(original))?;
74+
let file_path = thumb_dir.as_ref().join(original);
7575
7676
Ok(img.resize(longest_edge, longest_edge, FilterType::Nearest)
77-
.save(fout, image::JPEG)?)
77+
.save(file_path)?)
7878
}
7979
#
8080
# quick_main!(run);

src/development_tools/debugging/config_log/log-custom.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Configures log to be output into custom location with [log4rs]. [log4rs] can use
88
Firstly creates the log configuration with [`log4rs::append::file::FileAppender`]
99
using a custom pattern from [`log4rs::encode::pattern`].
1010

11-
Secondly assigns it to the [`log4rs::config::Config`] which has a root appender that uses the previously created `logfile` appender. Subsequently sets the default [`log::LogLevelFilter`] so that any logs with `Info` level or higher will be sent to the logger.
11+
Secondly assigns it to the [`log4rs::config::Config`] which has a root appender that uses the previously created `logfile` appender. Subsequently sets the default [`log::LevelFilter`] so that any logs with `Info` level or higher will be sent to the logger.
1212

1313
```rust,no_run
1414
# #[macro_use]
@@ -17,7 +17,7 @@ Secondly assigns it to the [`log4rs::config::Config`] which has a root appender
1717
extern crate log;
1818
extern crate log4rs;
1919
20-
use log::LogLevelFilter;
20+
use log::LevelFilter;
2121
use log4rs::append::file::FileAppender;
2222
use log4rs::encode::pattern::PatternEncoder;
2323
use log4rs::config::{Appender, Config, Root};
@@ -39,7 +39,7 @@ fn run() -> Result<()> {
3939
.appender(Appender::builder().build("logfile", Box::new(logfile)))
4040
.build(Root::builder()
4141
.appender("logfile")
42-
.build(LogLevelFilter::Info))?;
42+
.build(LevelFilter::Info))?;
4343
4444
log4rs::init_config(config)?;
4545
@@ -54,4 +54,4 @@ fn run() -> Result<()> {
5454
[`log4rs::append::file::FileAppender`]: https://docs.rs/log4rs/*/log4rs/append/file/struct.FileAppender.html
5555
[`log4rs::config::Config`]: https://docs.rs/log4rs/*/log4rs/config/struct.Config.html
5656
[`log4rs::encode::pattern`]: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
57-
[`log::LogLevelFilter`]: https://doc.rust-lang.org/log/log/enum.LogLevelFilter.html
57+
[`log::LevelFilter`]: https://docs.rs/log/*/log/enum.LevelFilter.html

src/development_tools/debugging/config_log/log-env-variable.md

+11-23
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,34 @@
33

44
[![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
55

6-
Logging is configured with [`LogBuilder`].
6+
Logging is configured with [`Builder`].
77

8-
[`LogBuilder::parse`] parses `MY_APP_LOG`
8+
[`Builder::parse`] parses `MY_APP_LOG`
99
environmental variable contents in the form of [`RUST_LOG`] syntax.
10-
Then [`LogBuilder::init`] initializes the logger.
10+
Then [`Builder::init`] initializes the logger.
1111
All these steps are normally done internally by [`env_logger::init`].
1212

1313
```rust
14-
# #[macro_use]
15-
# extern crate error_chain;
1614
#[macro_use]
1715
extern crate log;
1816
extern crate env_logger;
1917

2018
use std::env;
21-
use env_logger::LogBuilder;
22-
23-
# error_chain! {
24-
# foreign_links {
25-
# EnvLogger(log::SetLoggerError);
26-
# }
27-
# }
28-
#
29-
fn run() -> Result<()> {
30-
LogBuilder::new()
19+
use env_logger::Builder;
20+
21+
fn main() {
22+
Builder::new()
3123
.parse(&env::var("MY_APP_LOG").unwrap_or_default())
32-
.init()?;
24+
.init();
3325

3426
info!("informational message");
3527
warn!("warning message");
3628
error!("this is an error {}", "message");
37-
38-
Ok(())
3929
}
40-
#
41-
# quick_main!(run);
4230
```
4331

4432
[`env_logger::init`]: https://doc.rust-lang.org/log/env_logger/fn.init.html
45-
[`LogBuilder`]: https://doc.rust-lang.org/log/env_logger/struct.Builder.html
46-
[`LogBuilder::init`]: https://doc.rust-lang.org/log/env_logger/struct.LogBuilder.html#method.init
47-
[`LogBuilder::parse`]: https://doc.rust-lang.org/log/env_logger/struct.LogBuilder.html#method.parse
33+
[`Builder`]: https://docs.rs/env_logger/*/env_logger/struct.Builder.html
34+
[`Builder::init`]: https://docs.rs/env_logger/*/env_logger/struct.Builder.html#method.init
35+
[`Builder::parse`]: https://docs.rs/env_logger/*/env_logger/struct.Builder.html#method.parsese
4836
[`RUST_LOG`]: https://doc.rust-lang.org/log/env_logger/#enabling-logging

src/development_tools/debugging/config_log/log-mod.md

+4-16
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ Creates two modules `foo` and nested `foo::bar` with logging directives
77
controlled separately with [`RUST_LOG`] environmental variable.
88

99
```rust
10-
# #[macro_use]
11-
# extern crate error_chain;
1210
#[macro_use]
1311
extern crate log;
1412
extern crate env_logger;
@@ -29,24 +27,14 @@ mod foo {
2927
bar::run();
3028
}
3129
}
32-
#
33-
# error_chain! {
34-
# foreign_links {
35-
# SetLogger(log::SetLoggerError);
36-
# }
37-
# }
3830

39-
fn run() -> Result<()> {
40-
env_logger::init()?;
31+
fn main() {
32+
env_logger::init();
4133
warn!("[root] warn");
4234
info!("[root] info");
4335
debug!("[root] debug");
4436
foo::run();
45-
46-
Ok(())
4737
}
48-
#
49-
# quick_main!(run);
5038
```
5139

5240
[`env_logger`][env_logger] output is controlled by [`RUST_LOG`] environmental
@@ -57,7 +45,7 @@ Running the `test` application as follows:
5745
RUST_LOG="warn,test::foo=info,test::foo::bar=debug" ./test
5846
```
5947

60-
Sets the default [`log::LogLevel`] to `warn`, module's `foo` and module's `foo::bar`
48+
Sets the default [`log::Level`] to `warn`, module's `foo` and module's `foo::bar`
6149
respectively to `info` and `debug`. The output is:
6250

6351
```bash
@@ -69,5 +57,5 @@ INFO:test::foo::bar: [bar] info
6957
DEBUG:test::foo::bar: [bar] debug
7058
```
7159

72-
[`log::LogLevel`]: https://doc.rust-lang.org/log/log/enum.LogLevel.html
60+
[`log::Level`]: https://docs.rs/log/*/log/enum.Level.html
7361
[`RUST_LOG`]: https://doc.rust-lang.org/log/env_logger/#enabling-logging

src/development_tools/debugging/config_log/log-timestamp.md

+22-31
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,40 @@
33

44
[![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] [![cat-debugging-badge]][cat-debugging]
55

6-
Creates a custom logger configuration with [`LogBuilder`].
6+
Creates a custom logger configuration with [`Builder`].
77
Each log entry calls [`Local::now`] to get the current [`DateTime`] in local timezone and uses [`DateTime::format`] with [`strftime::specifiers`] to format a timestamp used in the final log.
88

9-
The example calls [`LogBuilder::format`] to set a closure which formats each
10-
message text with timestamp, [`LogRecord::level`] and body ([`LogRecord::args`]).
9+
The example calls [`Builder::format`] to set a closure which formats each
10+
message text with timestamp, [`Record::level`] and body ([`Record::args`]).
1111

1212
```rust
13-
# #[macro_use]
14-
# extern crate error_chain;
1513
#[macro_use]
1614
extern crate log;
17-
extern crate env_logger;
1815
extern crate chrono;
16+
extern crate env_logger;
1917

2018
use std::env;
21-
use env_logger::LogBuilder;
19+
use std::io::Write;
2220
use chrono::Local;
23-
24-
#
25-
# error_chain! {
26-
# foreign_links {
27-
# SetLogger(log::SetLoggerError);
28-
# }
29-
# }
30-
31-
fn run() -> Result<()> {
32-
LogBuilder::new()
33-
.format(|record| {
34-
format!("{} [{}] - {}",
35-
Local::now().format("%Y-%m-%dT%H:%M:%S"),
36-
record.level(),
37-
record.args())
38-
})
21+
use env_logger::Builder;
22+
23+
fn main() {
24+
Builder::new()
25+
.format(|buf, record| {
26+
write!(buf,
27+
"{} [{}] - {}",
28+
Local::now().format("%Y-%m-%dT%H:%M:%S"),
29+
record.level(),
30+
record.args()
31+
)
32+
})
3933
.parse(&env::var("MY_APP_LOG").unwrap_or_default())
40-
.init()?;
34+
.init();
4135

4236
warn!("warn");
4337
info!("info");
4438
debug!("debug");
45-
Ok(())
4639
}
47-
#
48-
# quick_main!(run);
4940
```
5041
Calling `MY_APP_LOG="info" cargo run` will result in similar output:
5142
```
@@ -56,8 +47,8 @@ Calling `MY_APP_LOG="info" cargo run` will result in similar output:
5647
[`DateTime::format`]: https://docs.rs/chrono/*/chrono/datetime/struct.DateTime.html#method.format
5748
[`DateTime`]: https://docs.rs/chrono/*/chrono/datetime/struct.DateTime.html
5849
[`Local::now`]: https://docs.rs/chrono/*/chrono/offset/local/struct.Local.html#method.now
59-
[`LogBuilder`]: https://doc.rust-lang.org/log/env_logger/struct.Builder.html
60-
[`LogBuilder::format`]: https://doc.rust-lang.org/log/env_logger/struct.LogBuilder.html#method.format
61-
[`LogRecord::args`]: https://doc.rust-lang.org/log/log/struct.LogRecord.html#method.args
62-
[`LogRecord::level`]: https://doc.rust-lang.org/log/log/struct.LogRecord.html#method.level
50+
[`Builder`]: https://docs.rs/env_logger/*/env_logger/struct.Builder.html
51+
[`Builder::format`]: https://docs.rs/env_logger/*/env_logger/struct.Builder.html#method.format
52+
[`Record::args`]: https://docs.rs/log/*/log/struct.Record.html#method.args
53+
[`Record::level`]: https://docs.rs/log/*/log/struct.Record.html#method.level
6354
[`strftime::specifiers`]: https://docs.rs/chrono/*/chrono/format/strftime/index.html#specifiers

src/development_tools/debugging/log/log-custom-logger.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ the [`log::Log`] trait and has to be installed via [`log::set_logger`].
1313
#[macro_use]
1414
extern crate log;
1515

16-
use log::{LogRecord, LogLevel, LogMetadata, LogLevelFilter};
16+
use log::{Record, Level, Metadata, LevelFilter};
17+
18+
static CONSOLE_LOGGER: ConsoleLogger = ConsoleLogger;
1719

1820
struct ConsoleLogger;
1921

2022
impl log::Log for ConsoleLogger {
21-
fn enabled(&self, metadata: &LogMetadata) -> bool {
22-
metadata.level() <= LogLevel::Info
23+
fn enabled(&self, metadata: &Metadata) -> bool {
24+
metadata.level() <= Level::Info
2325
}
2426

25-
fn log(&self, record: &LogRecord) {
27+
fn log(&self, record: &Record) {
2628
if self.enabled(record.metadata()) {
2729
println!("Rust says: {} - {}", record.level(), record.args());
2830
}
2931
}
32+
33+
fn flush(&self) {}
3034
}
3135
#
3236
# error_chain! {
@@ -36,10 +40,8 @@ impl log::Log for ConsoleLogger {
3640
# }
3741

3842
fn run() -> Result<()> {
39-
log::set_logger(|max_log_level| {
40-
max_log_level.set(LogLevelFilter::Info);
41-
Box::new(ConsoleLogger)
42-
})?;
43+
log::set_logger(&CONSOLE_LOGGER)?;
44+
log::set_max_level(LevelFilter::Info);
4345

4446
info!("hello log");
4547
warn!("warning");

src/development_tools/debugging/log/log-debug.md

+2-16
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,16 @@ logging via an environment variable.
1010
#[macro_use]
1111
extern crate log;
1212
extern crate env_logger;
13-
#
14-
# #[macro_use]
15-
# extern crate error_chain;
16-
#
17-
# error_chain! {
18-
# foreign_links {
19-
# SetLogger(log::SetLoggerError);
20-
# }
21-
# }
2213

2314
fn execute_query(query: &str) {
2415
debug!("Executing query: {}", query);
25-
2616
}
2717

28-
fn run() -> Result<()> {
29-
env_logger::init()?;
18+
fn main {
19+
env_logger::init();
3020

3121
execute_query("DROP TABLE students");
32-
33-
Ok(())
3422
}
35-
#
36-
# quick_main!(run);
3723
```
3824

3925
If you run this code, you'll notice that no output is printed. By default, the

src/development_tools/debugging/log/log-error.md

+2-15
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,22 @@
77
#[macro_use]
88
extern crate log;
99
extern crate env_logger;
10-
#
11-
# #[macro_use]
12-
# extern crate error_chain;
13-
#
14-
# error_chain! {
15-
# foreign_links {
16-
# SetLogger(log::SetLoggerError);
17-
# }
18-
# }
1910

2011
fn execute_query(_query: &str) -> Result<()> {
2112
// Do the thing, or maybe not
2213

2314
bail!("I'm afraid I can't do that")
2415
}
2516

26-
fn run() -> Result<()> {
27-
env_logger::init()?;
17+
fn main() {
18+
env_logger::init();
2819

2920
let response = execute_query("DROP TABLE students");
3021
if let Err(err) = response {
3122
// Log the error message and continue
3223
error!("Failed to execute query: {}", err);
3324
}
34-
35-
Ok(())
3625
}
37-
#
38-
# quick_main!(run);
3926
```
4027

4128
Run this code with `cargo run` and you should see the following line:

0 commit comments

Comments
 (0)