Document error cases and edge-case behavior of IndexedRandom sampling methods#1811
Open
teddytennant wants to merge 1 commit into
Open
Document error cases and edge-case behavior of IndexedRandom sampling methods#1811teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
… methods - choose_weighted, choose_weighted_mut, choose_weighted_iter: document that error cases are those of WeightedIndex::new, with doc-test assertions for the empty-slice and all-zero-weights cases - sample_weighted: document the InvalidWeight error case (matching index::sample_weighted) and that amount is clamped to self.len() - sample: document that amount is clamped to self.len(), with a doc-test - sample_array: document that None is returned iff N > self.len(), with a doc-test Follow-up to rust-random#1783.
dhardy
requested changes
Jul 13, 2026
dhardy
left a comment
Member
There was a problem hiding this comment.
Two lines are marginally useful. Most of this is just noise. (To a human who is able to read the docs in the intended docs.rs presentation format, including following links.)
| /// Chooses `amount` elements from the slice at random, without repetition, | ||
| /// and in random order. The returned iterator is appropriate both for | ||
| /// collection into a `Vec` and filling an existing buffer (see example). | ||
| /// If `amount > self.len()`, `amount` is reduced to `self.len()`. |
Member
There was a problem hiding this comment.
Suggested change
| /// If `amount > self.len()`, `amount` is reduced to `self.len()`. | |
| /// If `amount > self.len()`, all available elements are sampled. |
Comment on lines
+112
to
+114
| /// | ||
| /// // if the requested amount exceeds the length, every element is sampled: | ||
| /// assert_eq!(sample.sample(&mut rng, 1000).count(), sample.len()); |
Member
There was a problem hiding this comment.
Suggested change
| /// | |
| /// // if the requested amount exceeds the length, every element is sampled: | |
| /// assert_eq!(sample.sample(&mut rng, 1000).count(), sample.len()); |
Comment on lines
+145
to
+148
| /// | ||
| /// // None is returned when more elements are requested than are available: | ||
| /// let b: Option<[u8; 32]> = sample.sample_array(&mut rng); | ||
| /// assert!(b.is_none()); |
Member
There was a problem hiding this comment.
Suggested change
| /// | |
| /// // None is returned when more elements are requested than are available: | |
| /// let b: Option<[u8; 32]> = sample.sample_array(&mut rng); | |
| /// assert!(b.is_none()); |
Comment on lines
+172
to
+175
| /// Error cases are those of [`WeightedIndex::new`]; in particular, the | ||
| /// slice being empty results in [`WeightError::InvalidInput`] and all | ||
| /// weights being zero in [`WeightError::InsufficientNonZero`]. | ||
| /// |
Member
There was a problem hiding this comment.
This is already documented by the return-type.
Suggested change
| /// Error cases are those of [`WeightedIndex::new`]; in particular, the | |
| /// slice being empty results in [`WeightError::InvalidInput`] and all | |
| /// weights being zero in [`WeightError::InsufficientNonZero`]. | |
| /// |
| /// | ||
| /// ``` | ||
| /// use rand::prelude::*; | ||
| /// use rand::seq::WeightError; |
Member
There was a problem hiding this comment.
Suggested change
| /// use rand::seq::WeightError; |
Comment on lines
+229
to
+230
| /// | ||
| /// [`WeightedIndex::new`]: crate::distr::weighted::WeightedIndex::new |
Member
There was a problem hiding this comment.
Suggested change
| /// | |
| /// [`WeightedIndex::new`]: crate::distr::weighted::WeightedIndex::new |
| /// elements are never returned; the result may therefore contain fewer | ||
| /// elements than `amount` even when `self.len() >= amount`. The elements | ||
| /// are returned in an arbitrary, unspecified order. | ||
| /// If `amount > self.len()`, `amount` is reduced to `self.len()`. |
Member
There was a problem hiding this comment.
This behaviour is documented in sample which is linked here.
Suggested change
| /// If `amount > self.len()`, `amount` is reduced to `self.len()`. |
Comment on lines
+261
to
+263
| /// Error cases: | ||
| /// - [`WeightError::InvalidWeight`] when a weight is not-a-number or negative. | ||
| /// |
Member
There was a problem hiding this comment.
Suggested change
| /// Error cases: | |
| /// - [`WeightError::InvalidWeight`] when a weight is not-a-number or negative. | |
| /// |
Comment on lines
+391
to
+394
| /// Error cases are those of [`WeightedIndex::new`]; in particular, the | ||
| /// slice being empty results in [`WeightError::InvalidInput`] and all | ||
| /// weights being zero in [`WeightError::InsufficientNonZero`]. | ||
| /// |
Member
There was a problem hiding this comment.
Suggested change
| /// Error cases are those of [`WeightedIndex::new`]; in particular, the | |
| /// slice being empty results in [`WeightError::InvalidInput`] and all | |
| /// weights being zero in [`WeightError::InsufficientNonZero`]. | |
| /// |
| /// [`choose_mut`]: IndexedMutRandom::choose_mut | ||
| /// [`choose_weighted`]: IndexedRandom::choose_weighted | ||
| /// [`WeightedIndex`]: crate::distr::weighted::WeightedIndex | ||
| /// [`WeightedIndex::new`]: crate::distr::weighted::WeightedIndex::new |
Member
There was a problem hiding this comment.
Suggested change
| /// [`WeightedIndex::new`]: crate::distr::weighted::WeightedIndex::new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Documents the error cases and edge-case behavior of the
IndexedRandomsampling methods insrc/seq/slice.rs, with doc-test assertions where the behavior is easy to demonstrate:choose_weighted/choose_weighted_mut/choose_weighted_iter: state that error cases are those ofWeightedIndex::new— in particularWeightError::InvalidInputfor an empty slice andWeightError::InsufficientNonZerowhen all weights are zero. Thechoose_weightedexample now asserts both.sample_weighted: document theWeightError::InvalidWeightcase (matching the wording onindex::sample_weighted) and thatamountis clamped toself.len().sample: document thatamountis clamped toself.len(), with a doc-test assertingsample(&mut rng, 1000).count() == sample.len().sample_array: document thatNoneis returned if (and only if)N > self.len(), with a doc-test.Why
Follow-up to #1783. That issue was about
choose_weightedclaiming to returnNonefor empty sequences when it actually returnsErr(WeightError::InvalidInput); the wrong claim was removed, but the docs were left saying nothing about when these methods error. Similarly, the clamping behavior ofsample/sample_weightedand theNonecondition ofsample_arraywere implemented but undocumented, so a reader had to consult the source (orindex::sample*) to learn them.Testing
cargo test --doc seq::slice— all 9 doc-tests pass, including the new/extended ones.cargo fmt --checkandcargo clippy --lib— clean.Doc-only change; no function bodies are touched.