You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
should read after deciding which crate suites your purpose.</p>
232
233
<h2id="a-note-about-error-handling"><aclass="header" href="#a-note-about-error-handling">A note about error handling</a></h2>
233
234
<p>Rust has <ahref="https://doc.rust-lang.org/std/error/trait.Error.html"><code>std::error::Trait</code></a> which is implemented to handle exceptions.
234
-
Handling multiple types of these traits can be simplified using <ahref="https://docs.rs/anyhow/latest/anyhow/"><code>anyhow</code></a>
235
-
or specified with an <code>enum</code> which macros exist to make this easier within
236
-
<ahref="https://docs.rs/thiserror/latest/thiserror/"><code>thiserror</code></a> for library authors.</p>
237
-
<p>Error chain has been shown in this book for historical reasons before Rust
238
-
<code>std</code> and crates represented macro use as a preference. For more background
239
-
on error handling in Rust, read <ahref="https://doc.rust-lang.org/book/ch09-00-error-handling.html">this page of the Rust book</a>
240
-
and <ahref="https://brson.github.io/2016/11/30/starting-with-error-chain">this blog post</a>.</p>
235
+
This cookbook uses <ahref="https://docs.rs/anyhow/latest/anyhow/"><code>anyhow</code></a> for simplified error handling in examples,
236
+
which provides easy error propagation and context. For library authors,
237
+
<ahref="https://docs.rs/thiserror/latest/thiserror/"><code>thiserror</code></a> provides a more structured approach using derive macros
238
+
to create custom error types.</p>
239
+
<p>This cookbook previously used the <code>error-chain</code> crate, but has been updated
240
+
to use <code>anyhow</code> as it's now the preferred approach for application-level
241
+
error handling. For more background on error handling in Rust, read
242
+
<ahref="https://doc.rust-lang.org/book/ch09-00-error-handling.html">this page of the Rust book</a> and <ahref="https://brson.github.io/2016/11/30/starting-with-error-chain">this blog post</a>.</p>
241
243
<h2id="a-note-about-crate-representation"><aclass="header" href="#a-note-about-crate-representation">A note about crate representation</a></h2>
242
244
<p>This cookbook is intended eventually to provide expansive coverage of
243
245
the Rust crate ecosystem, but today is limited in scope while we get
<p>Generates random numbers with help of random-number
166
-
generator <ahref="https://docs.rs/rand/*/rand/trait.Rng.html"><code>rand::Rng</code></a> obtained via <ahref="https://docs.rs/rand/*/rand/trait.Rng.html"><code>rand::rng</code></a>. Each thread has an
166
+
generator <ahref="https://docs.rs/rand/0.9/rand/trait.Rng.html"><code>rand::Rng</code></a> obtained via <ahref="https://docs.rs/rand/0.9/rand/trait.Rng.html"><code>rand::rng</code></a>. Each thread has an
167
167
initialized generator. Integers are uniformly distributed over the range of the
168
168
type, and floating point numbers are uniformly distributed from 0 up to but not
<h2id="generate-random-numbers-within-a-range"><aclass="header" href="#generate-random-numbers-within-a-range">Generate random numbers within a range</a></h2>
<p>Generates a random value within half-open <code>[0, 10)</code> range (not including <code>10</code>) with <ahref="https://doc.rust-lang.org/rand/*/rand/trait.Rng.html#method.random_range"><code>Rng::random_range</code></a>.</p>
179
+
<p>Generates a random value within half-open <code>[0, 10)</code> range (not including <code>10</code>) with [<code>Rng::gen_range</code>].</p>
<p><ahref="https://docs.rs/rand/*/rand/distributions/uniform/struct.Uniform.html"><code>Uniform</code></a> can obtain values with <ahref="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>.
187
+
<p>[<code>Uniform</code>] can obtain values with <ahref="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>.
194
188
This has the same effect, but may be faster when repeatedly generating numbers
<ahref="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>. The <ahref="https://docs.rs/rand_distr/*/rand_distr/index.html"><code>rand_distr</code></a> crate provides
215
210
other kinds of distributions. To use them, you instantiate
216
211
a distribution, then sample from that distribution using
217
-
<ahref="https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample"><code>Distribution::sample</code></a> with help of a random-number
<ahref="https://docs.rs/rand/0.9/rand/distr/trait.Distribution.html#tymethod.sample"><code>Distribution::sample</code></a> with help of a random-number
use rand_distr::{Distribution, LogNormal, Normal};
218
+
219
+
fn main() {
220
+
let mut rng = rand::rng();
221
+
let normal = Normal::new(2.0, 3.0)
222
+
.expect("Failed to create normal distribution");
223
+
let log_normal = LogNormal::new(1.0, 0.5)
224
+
.expect("Failed to create log-normal distribution");
223
225
224
-
fn main() -> Result<(), NormalError> {
225
-
let mut rng = rng();
226
-
let normal = Normal::new(2.0, 3.0)?;
227
226
let v = normal.sample(&mut rng);
228
227
println!("{} is from a N(2, 9) distribution", v);
229
-
Ok(())
230
-
}</code></pre>
228
+
let v = log_normal.sample(&mut rng);
229
+
println!("{} is from an ln N(1, 0.25) distribution", v);
230
+
}</code></pre></pre>
231
231
<h2id="generate-random-values-of-a-custom-type"><aclass="header" href="#generate-random-values-of-a-custom-type">Generate random values of a custom type</a></h2>
<p>Randomly generates a tuple <code>(i32, bool, f64)</code> and variable of user defined type <code>Point</code>.
234
-
Implements the <ahref="https://docs.rs/rand/*/rand/distributions/trait.Distribution.html"><code>Distribution</code></a> trait on type Point for <ahref="https://docs.rs/rand/*/rand/distributions/struct.Standard.html"><code>Standard</code></a> in order to allow random generation.</p>
234
+
Implements the [<code>Distribution</code>] trait on type Point for [<code>Standard</code>] in order to allow random generation.</p>
let rand_tuple = rng.random::<(i32, bool, f64)>();
257
-
let rand_point: Point = rng.random();
255
+
let rand_point = Point::random(&mut rng);
258
256
println!("Random tuple: {:?}", rand_tuple);
259
257
println!("Random Point: {:?}", rand_point);
260
258
}</code></pre></pre>
261
259
<h2id="create-random-passwords-from-a-set-of-alphanumeric-characters"><aclass="header" href="#create-random-passwords-from-a-set-of-alphanumeric-characters">Create random passwords from a set of alphanumeric characters</a></h2>
<p>Randomly generates a string of given length ASCII characters in the range <code>A-Z, a-z, 0-9</code>, with <ahref="https://docs.rs/rand/*/rand/distributions/struct.Alphanumeric.html"><code>Alphanumeric</code></a> sample.</p>
<p>Randomly generates a string of given length ASCII characters in the range <code>A-Z, a-z, 0-9</code>, with <ahref="https://docs.rs/rand/0.9/rand/distr/struct.Alphanumeric.html"><code>Alphanumeric</code></a> sample.</p>
<h2id="create-random-passwords-from-a-set-of-user-defined-characters"><aclass="header" href="#create-random-passwords-from-a-set-of-user-defined-characters">Create random passwords from a set of user-defined characters</a></h2>
under <code>backup/logs</code> path with <ahref="https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all"><code>Builder::append_dir_all</code></a>.
190
189
<ahref="https://docs.rs/flate2/*/flate2/write/struct.GzEncoder.html"><code>GzEncoder</code></a> is responsible for transparently compressing the
191
190
data prior to writing it into <code>archive.tar.gz</code>.</p>
<p>To add the contents without renaming them, an empty string can be used as the first argument of <ahref="https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all"><code>Builder::append_dir_all</code></a>:</p>
<p>Iterate over the <ahref="https://docs.rs/tar/*/tar/struct.Archive.html#method.entries"><code>Archive::entries</code></a>. Use <ahref="https://doc.rust-lang.org/std/path/struct.Path.html#method.strip_prefix"><code>Path::strip_prefix</code></a> to remove
224
221
the specified path prefix (<code>bundle/logs</code>). Finally, extract the <ahref="https://docs.rs/tar/*/tar/struct.Entry.html"><code>tar::Entry</code></a>
225
222
via <ahref="https://docs.rs/tar/*/tar/struct.Entry.html#method.unpack"><code>Entry::unpack</code></a>.</p>
0 commit comments