mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-17 12:48:29 +00:00
Expand recipes algorithm through development tools
This commit is contained in:
parent
2a57fa3904
commit
db0ee589d4
15 changed files with 93 additions and 83 deletions
|
@ -7,8 +7,8 @@ the specified path prefix (`bundle/logs`). Finally, extract the [`tar::Entry`]
|
|||
via [`Entry::unpack`].
|
||||
|
||||
```rust,no_run
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
extern crate flate2;
|
||||
extern crate tar;
|
||||
|
||||
|
@ -16,13 +16,13 @@ use std::fs::File;
|
|||
use std::path::PathBuf;
|
||||
use flate2::read::GzDecoder;
|
||||
use tar::Archive;
|
||||
#
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# Io(std::io::Error);
|
||||
# StripPrefixError(::std::path::StripPrefixError);
|
||||
# }
|
||||
# }
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(std::io::Error);
|
||||
StripPrefixError(::std::path::StripPrefixError);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let file = File::open("archive.tar.gz")?;
|
||||
|
|
|
@ -9,8 +9,8 @@ then saves them in a new folder called `thumbnails`.
|
|||
images in parallel using [`par_iter`] calling [`DynamicImage::resize`].
|
||||
|
||||
```rust,no_run
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
extern crate glob;
|
||||
extern crate image;
|
||||
extern crate rayon;
|
||||
|
@ -18,18 +18,18 @@ extern crate rayon;
|
|||
use std::path::Path;
|
||||
use std::fs::create_dir_all;
|
||||
|
||||
# use error_chain::ChainedError;
|
||||
use error_chain::ChainedError;
|
||||
use glob::{glob_with, MatchOptions};
|
||||
use image::{FilterType, ImageError};
|
||||
use rayon::prelude::*;
|
||||
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# Image(ImageError);
|
||||
# Io(std::io::Error);
|
||||
# Glob(glob::PatternError);
|
||||
# }
|
||||
# }
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Image(ImageError);
|
||||
Io(std::io::Error);
|
||||
Glob(glob::PatternError);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let options: MatchOptions = Default::default();
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
# Encryption
|
||||
|
||||
{{#include encryption/pbkdf2.md}}
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
|
|
@ -4,4 +4,6 @@
|
|||
|
||||
{{#include hashing/hmac.md}}
|
||||
|
||||
{{#include encryption/pbkdf2.md}}
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
|
|
@ -2,7 +2,11 @@
|
|||
|
||||
[![postgres-badge]][postgres] [![cat-database-badge]][cat-database]
|
||||
|
||||
This recipe lists the nationalities of the first 7999 artists in the database of the [`Museum of Modern Art`] in descending order.
|
||||
This recipe lists the nationalities of the first 7999 artists in the database of the [`Museum of Modern Art`] in
|
||||
descending order.
|
||||
|
||||
In this example, a local postgres instance rows in a Nation struct. A GROUP BY clause collects the artists into their
|
||||
nationality.
|
||||
|
||||
```rust,no_run
|
||||
extern crate postgres;
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
|
||||
Use the [`postgres`] crate to create tables in a Postgres database.
|
||||
|
||||
[`Connection::connect`] helps in connecting to an existing database. The recipe uses a URL string format with `Connection::connect`. It assumes an existing database named `library`, the username is `postgres` and the password is `postgres`.
|
||||
[`Connection::connect`] connects to an existing database. The recipe uses a URL string format with `Connection::connect`.
|
||||
The recipe requires an existing database named `library`, with a `postgres` user with `postgres` password.
|
||||
|
||||
```rust,no_run
|
||||
extern crate postgres;
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
[![postgres-badge]][postgres] [![cat-database-badge]][cat-database]
|
||||
|
||||
The recipe inserts data into the `author` table using [`execute`] method of `Connection`. Then, displays the data from the `author` table using [`query`] method of `Connection`.
|
||||
The recipe inserts data into the `author` table using [`execute`] method of `Connection`.
|
||||
Then, displays the data from the `author` table using [`query`] method of `Connection`.
|
||||
|
||||
```rust,no_run
|
||||
extern crate postgres;
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
[![rusqlite-badge]][rusqlite] [![cat-database-badge]][cat-database]
|
||||
|
||||
Use the `rusqlite` crate to open SQLite databases. See
|
||||
[crate][documentation] for compiling on Windows.
|
||||
[crate][documentation] for compiling on Windows. SQLite databases are single-file and do not require a database
|
||||
management service to run.
|
||||
|
||||
[`Connection::open`] will create the database if it doesn't already exist.
|
||||
|
||||
|
|
|
@ -9,11 +9,11 @@ This method will not mutate or reset the [`time::Instant`] object.
|
|||
|
||||
```rust
|
||||
use std::time::{Duration, Instant};
|
||||
# use std::thread;
|
||||
#
|
||||
# fn expensive_function() {
|
||||
# thread::sleep(Duration::from_secs(1));
|
||||
# }
|
||||
use std::thread;
|
||||
|
||||
fn expensive_function() {
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
|
|
|
@ -58,25 +58,25 @@ void greet(const char* name) {
|
|||
### `src/main.rs`
|
||||
|
||||
```rust,ignore
|
||||
# #[macro_use] extern crate error_chain;
|
||||
#[macro_use] extern crate error_chain;
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::c_char;
|
||||
#
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# NulError(::std::ffi::NulError);
|
||||
# Io(::std::io::Error);
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# fn prompt(s: &str) -> Result<String> {
|
||||
# use std::io::Write;
|
||||
# print!("{}", s);
|
||||
# std::io::stdout().flush()?;
|
||||
# let mut input = String::new();
|
||||
# std::io::stdin().read_line(&mut input)?;
|
||||
# Ok(input.trim().to_string())
|
||||
# }
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
NulError(::std::ffi::NulError);
|
||||
Io(::std::io::Error);
|
||||
}
|
||||
}
|
||||
|
||||
fn prompt(s: &str) -> Result<String> {
|
||||
use std::io::Write;
|
||||
print!("{}", s);
|
||||
std::io::stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input)?;
|
||||
Ok(input.trim().to_string())
|
||||
}
|
||||
|
||||
extern {
|
||||
fn hello();
|
||||
|
|
|
@ -12,8 +12,8 @@ Assigns the configuration to [`log4rs::config::Config`] and sets the default
|
|||
[`log::LevelFilter`].
|
||||
|
||||
```rust,no_run
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate log4rs;
|
||||
|
@ -22,14 +22,14 @@ use log::LevelFilter;
|
|||
use log4rs::append::file::FileAppender;
|
||||
use log4rs::encode::pattern::PatternEncoder;
|
||||
use log4rs::config::{Appender, Config, Root};
|
||||
#
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# Io(std::io::Error);
|
||||
# LogConfig(log4rs::config::Errors);
|
||||
# SetLogger(log::SetLoggerError);
|
||||
# }
|
||||
# }
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(std::io::Error);
|
||||
LogConfig(log4rs::config::Errors);
|
||||
SetLogger(log::SetLoggerError);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let logfile = FileAppender::builder()
|
||||
|
|
|
@ -7,16 +7,18 @@ with [`syslog::init`]. [`syslog::Facility`] records the program submitting
|
|||
the log entry's classification, [`log::LevelFilter`] denotes allowed log verbosity
|
||||
and `Option<&str>` holds optional application name.
|
||||
|
||||
This recipe demonstrates filtering crates, functions, and use statements by the operating system with `#[cfg(target_os)]`
|
||||
|
||||
```rust
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
# #[cfg(target_os = "linux")]
|
||||
#[cfg(target_os = "linux")]
|
||||
extern crate syslog;
|
||||
|
||||
# #[cfg(target_os = "linux")]
|
||||
#[cfg(target_os = "linux")]
|
||||
use syslog::{Facility, Error};
|
||||
|
||||
# #[cfg(target_os = "linux")]
|
||||
#[cfg(target_os = "linux")]
|
||||
fn main() -> Result<(), Error> {
|
||||
syslog::init(Facility::LOG_USER,
|
||||
log::LevelFilter::Debug,
|
||||
|
@ -26,10 +28,10 @@ fn main() -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
# #[cfg(not(target_os = "linux"))]
|
||||
# fn main() {
|
||||
# println!("So far, only Linux systems are supported.");
|
||||
# }
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn main() {
|
||||
println!("So far, only Linux systems are supported.");
|
||||
}
|
||||
```
|
||||
|
||||
[`log::LevelFilter`]: https://docs.rs/log/*/log/enum.LevelFilter.html
|
||||
|
|
|
@ -8,21 +8,21 @@ Runs `git --version` using [`Command`], then parses the version number into a
|
|||
"git version x.y.z".
|
||||
|
||||
```rust,no_run
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
extern crate semver;
|
||||
|
||||
use std::process::Command;
|
||||
use semver::{Version, VersionReq};
|
||||
#
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# Io(std::io::Error);
|
||||
# Utf8(std::string::FromUtf8Error);
|
||||
# SemVer(semver::SemVerError);
|
||||
# SemVerReq(semver::ReqParseError);
|
||||
# }
|
||||
# }
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(std::io::Error);
|
||||
Utf8(std::string::FromUtf8Error);
|
||||
SemVer(semver::SemVerError);
|
||||
SemVerReq(semver::ReqParseError);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let version_constraint = "> 1.12.0";
|
||||
|
|
|
@ -7,18 +7,18 @@ Given a list of version &strs, finds the latest [`semver::Version`].
|
|||
Also demonstrates `semver` pre-release preferences.
|
||||
|
||||
```rust
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
extern crate semver;
|
||||
|
||||
use semver::{Version, VersionReq};
|
||||
#
|
||||
# error_chain! {
|
||||
# foreign_links {
|
||||
# SemVer(semver::SemVerError);
|
||||
# SemVerReq(semver::ReqParseError);
|
||||
# }
|
||||
# }
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
SemVer(semver::SemVerError);
|
||||
SemVerReq(semver::ReqParseError);
|
||||
}
|
||||
}
|
||||
|
||||
fn find_max_matching_version<'a, I>(version_req_str: &str, iterable: I) -> Result<Option<Version>>
|
||||
where
|
||||
|
|
Loading…
Add table
Reference in a new issue