Skip to content

Commit e9de258

Browse files
committed
IteratorRandom::sample: allow large "amount"
1 parent 8336923 commit e9de258

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1919
- Remove fns `SeedableRng::from_os_rng`, `try_from_os_rng` (#1674)
2020
- Remove `Clone` support for `StdRng`, `ReseedingRng` (#1677)
2121
- Use `postcard` instead of `bincode` to test the serde feature (#1693)
22+
- Avoid excessive allocation in `IteratorRandom::sample` when `amount` is much larger than iterator size
2223

2324
### Additions
2425
- Add fns `IndexedRandom::choose_iter`, `choose_weighted_iter` (#1632)

src/seq/iterator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ pub trait IteratorRandom: Iterator + Sized {
245245
where
246246
R: Rng + ?Sized,
247247
{
248-
let mut reservoir = Vec::with_capacity(amount);
249-
reservoir.extend(self.by_ref().take(amount));
248+
let mut reservoir = Vec::from_iter(self.by_ref().take(amount));
250249

251250
// Continue unless the iterator was exhausted
252251
//

0 commit comments

Comments
 (0)