Skip to content

Commit f3da1ba

Browse files
authored
Merge pull request #726 from dhardy/fmt
Add rustfmt configuration and accept a few of its changes
2 parents 5c4ece8 + b9e3e95 commit f3da1ba

7 files changed

Lines changed: 176 additions & 132 deletions

File tree

examples/monte-carlo.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//!
1212
//! Imagine that we have a square with sides of length 2 and a unit circle
1313
//! (radius = 1), both centered at the origin. The areas are:
14-
//!
14+
//!
1515
//! ```text
1616
//! area of circle = πr² = π * r * r = π
1717
//! area of square = 2² = 4
@@ -24,28 +24,28 @@
2424
//! the square at random, calculate the fraction that fall within the circle,
2525
//! and multiply this fraction by 4.
2626
27-
#![cfg(feature="std")]
27+
#![cfg(feature = "std")]
2828

2929

3030
extern crate rand;
3131

3232
use rand::distributions::{Distribution, Uniform};
3333

3434
fn main() {
35-
let range = Uniform::new(-1.0f64, 1.0);
36-
let mut rng = rand::thread_rng();
37-
38-
let total = 1_000_000;
39-
let mut in_circle = 0;
40-
41-
for _ in 0..total {
42-
let a = range.sample(&mut rng);
43-
let b = range.sample(&mut rng);
44-
if a*a + b*b <= 1.0 {
45-
in_circle += 1;
46-
}
47-
}
48-
49-
// prints something close to 3.14159...
50-
println!("π is approximately {}", 4. * (in_circle as f64) / (total as f64));
35+
let range = Uniform::new(-1.0f64, 1.0);
36+
let mut rng = rand::thread_rng();
37+
38+
let total = 1_000_000;
39+
let mut in_circle = 0;
40+
41+
for _ in 0..total {
42+
let a = range.sample(&mut rng);
43+
let b = range.sample(&mut rng);
44+
if a*a + b*b <= 1.0 {
45+
in_circle += 1;
46+
}
47+
}
48+
49+
// prints something close to 3.14159...
50+
println!("π is approximately {}", 4. * (in_circle as f64) / (total as f64));
5151
}

examples/monty-hall.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@
2626
//!
2727
//! [Monty Hall Problem]: https://en.wikipedia.org/wiki/Monty_Hall_problem
2828
29-
#![cfg(feature="std")]
29+
#![cfg(feature = "std")]
3030

3131

3232
extern crate rand;
3333

34-
use rand::Rng;
3534
use rand::distributions::{Distribution, Uniform};
35+
use rand::Rng;
3636

3737
struct SimulationResult {
3838
win: bool,
3939
switch: bool,
4040
}
4141

4242
// Run a single simulation of the Monty Hall problem.
43-
fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R)
44-
-> SimulationResult {
43+
fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult {
4544
let car = random_door.sample(rng);
4645

4746
// This is our initial choice

rustfmt.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This rustfmt file is added for configuration, but in practice much of our
2+
# code is hand-formatted, frequently with more readable results.
3+
4+
# Comments:
5+
normalize_comments = true
6+
wrap_comments = false
7+
format_doc_comments = true
8+
comment_width = 90 # small excess is okay but prefer 80
9+
10+
# Arguments:
11+
use_small_heuristics = "max"
12+
fn_args_density = "compressed"
13+
fn_single_line = false
14+
overflow_delimited_expr = true
15+
where_single_line = true
16+
17+
# enum_discrim_align_threshold = 20
18+
# struct_field_align_threshold = 20
19+
20+
# Compatibility:
21+
use_try_shorthand = true # stable since Rustc 1.13.0
22+
use_field_init_shorthand = true # stable since Rustc 1.17.0
23+
edition = "2015" # we require compatibility back to 1.22.0
24+
25+
# Misc:
26+
blank_lines_upper_bound = 2
27+
reorder_impl_items = true
28+
# report_todo = "Unnumbered"
29+
# report_fixme = "Unnumbered"
30+
31+
# Ignored files:
32+
ignore = []

src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ pub trait Rng: RngCore {
211211
/// println!("{:?}", rng.gen::<(f64, bool)>());
212212
/// ```
213213
#[inline]
214-
fn gen<T>(&mut self) -> T where Standard: Distribution<T> {
214+
fn gen<T>(&mut self) -> T
215+
where Standard: Distribution<T> {
215216
Standard.sample(self)
216217
}
217218

@@ -240,8 +241,10 @@ pub trait Rng: RngCore {
240241
///
241242
/// [`Uniform`]: distributions::uniform::Uniform
242243
fn gen_range<T: SampleUniform, B1, B2>(&mut self, low: B1, high: B2) -> T
243-
where B1: SampleBorrow<T> + Sized,
244-
B2: SampleBorrow<T> + Sized {
244+
where
245+
B1: SampleBorrow<T> + Sized,
246+
B2: SampleBorrow<T> + Sized,
247+
{
245248
T::Sampler::sample_single(low, high, self)
246249
}
247250

@@ -290,9 +293,10 @@ pub trait Rng: RngCore {
290293
/// println!("Not a 6; rolling again!");
291294
/// }
292295
/// ```
293-
fn sample_iter<'a, T, D: Distribution<T>>(&'a mut self, distr: &'a D)
294-
-> distributions::DistIter<'a, D, Self, T> where Self: Sized
295-
{
296+
fn sample_iter<'a, T, D: Distribution<T>>(
297+
&'a mut self, distr: &'a D,
298+
) -> distributions::DistIter<'a, D, Self, T>
299+
where Self: Sized {
296300
distr.sample_iter(self)
297301
}
298302

@@ -649,7 +653,8 @@ impl<R: SeedableRng> FromEntropy for R {
649653
/// [`Standard`]: distributions::Standard
650654
#[cfg(feature="std")]
651655
#[inline]
652-
pub fn random<T>() -> T where Standard: Distribution<T> {
656+
pub fn random<T>() -> T
657+
where Standard: Distribution<T> {
653658
thread_rng().gen()
654659
}
655660

src/seq/index.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,13 @@ impl ExactSizeIterator for IndexVecIntoIter {}
173173
/// Note that performance is significantly better over `u32` indices than over
174174
/// `u64` indices. Because of this we hide the underlying type behind an
175175
/// abstraction, `IndexVec`.
176-
///
176+
///
177177
/// If an allocation-free `no_std` function is required, it is suggested
178178
/// to adapt the internal `sample_floyd` implementation.
179179
///
180180
/// Panics if `amount > length`.
181181
pub fn sample<R>(rng: &mut R, length: usize, amount: usize) -> IndexVec
182-
where R: Rng + ?Sized,
183-
{
182+
where R: Rng + ?Sized {
184183
if amount > length {
185184
panic!("`amount` of samples must be less than or equal to `length`");
186185
}
@@ -227,8 +226,7 @@ pub fn sample<R>(rng: &mut R, length: usize, amount: usize) -> IndexVec
227226
///
228227
/// This implementation uses `O(amount)` memory and `O(amount^2)` time.
229228
fn sample_floyd<R>(rng: &mut R, length: u32, amount: u32) -> IndexVec
230-
where R: Rng + ?Sized,
231-
{
229+
where R: Rng + ?Sized {
232230
// For small amount we use Floyd's fully-shuffled variant. For larger
233231
// amounts this is slow due to Vec::insert performance, so we shuffle
234232
// afterwards. Benchmarks show little overhead from extra logic.
@@ -274,8 +272,7 @@ fn sample_floyd<R>(rng: &mut R, length: u32, amount: u32) -> IndexVec
274272
///
275273
/// Set-up is `O(length)` time and memory and shuffling is `O(amount)` time.
276274
fn sample_inplace<R>(rng: &mut R, length: u32, amount: u32) -> IndexVec
277-
where R: Rng + ?Sized,
278-
{
275+
where R: Rng + ?Sized {
279276
debug_assert!(amount <= length);
280277
let mut indices: Vec<u32> = Vec::with_capacity(length as usize);
281278
indices.extend(0..length);
@@ -290,13 +287,12 @@ fn sample_inplace<R>(rng: &mut R, length: u32, amount: u32) -> IndexVec
290287

291288
/// Randomly sample exactly `amount` indices from `0..length`, using rejection
292289
/// sampling.
293-
///
290+
///
294291
/// Since `amount <<< length` there is a low chance of a random sample in
295292
/// `0..length` being a duplicate. We test for duplicates and resample where
296293
/// necessary. The algorithm is `O(amount)` time and memory.
297294
fn sample_rejection<R>(rng: &mut R, length: usize, amount: usize) -> IndexVec
298-
where R: Rng + ?Sized,
299-
{
295+
where R: Rng + ?Sized {
300296
debug_assert!(amount < length);
301297
#[cfg(feature="std")] let mut cache = HashSet::with_capacity(amount);
302298
#[cfg(not(feature="std"))] let mut cache = BTreeSet::new();

0 commit comments

Comments
 (0)