Skip to content

Commit f531b81

Browse files
committed
docs: std::hash::Hash should ensure prefix-free data
Closes #89429
1 parent ed93759 commit f531b81

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/core/src/hash/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,19 @@ mod sip;
153153
/// Thankfully, you won't need to worry about upholding this property when
154154
/// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
155155
///
156+
/// ## Prefix collisions
157+
///
158+
/// Implementations of `hash` should ensure that the data they
159+
/// pass to the `Hasher` are prefix-free. That is, different concatenations
160+
/// of the same data should not produce the same output.
161+
/// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra
162+
/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a",
163+
/// "bc")` hash differently.
164+
///
156165
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
157166
/// [`HashSet`]: ../../std/collections/struct.HashSet.html
158167
/// [`hash`]: Hash::hash
168+
/// [impl]: ../../std/primitive.str.html#impl-Hash
159169
#[stable(feature = "rust1", since = "1.0.0")]
160170
#[rustc_diagnostic_item = "Hash"]
161171
pub trait Hash {

0 commit comments

Comments
 (0)