diff --git a/src/distributions/other.rs b/src/distributions/other.rs index 2e932703..7ce95ec3 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -94,6 +94,9 @@ impl Distribution for Standard { #[cfg(feature = "alloc")] impl DistString for Standard { fn append_string(&self, rng: &mut R, s: &mut String, len: usize) { + // A char is encoded with at most four bytes, thus this reservation is + // guaranteed to be sufficient. We do not shrink_to_fit afterwards so + // that repeated usage on the same `String` buffer does not reallocate. s.reserve(s.len() + 4 * len); s.extend(Distribution::::sample_iter(self, rng).take(len)); }