about summary refs log tree commit diff
diff options
context:
space:
mode:
authorpierwill <pierwill@users.noreply.github.com>2021-10-01 12:39:08 -0500
committerpierwill <pierwill@users.noreply.github.com>2021-10-01 12:41:22 -0500
commitf531b8122ebd7b07a50198b14727337763d05707 (patch)
treeb6d0bf6aad80d7e8888f80a904acfe20a27beaeb
parented937594d3912ced11f6f35a90bb8bf591909d2a (diff)
downloadrust-f531b8122ebd7b07a50198b14727337763d05707.tar.gz
rust-f531b8122ebd7b07a50198b14727337763d05707.zip
docs: `std::hash::Hash` should ensure prefix-free data
Closes #89429
-rw-r--r--library/core/src/hash/mod.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs
index da3f20d18e5..83cb85843b6 100644
--- a/library/core/src/hash/mod.rs
+++ b/library/core/src/hash/mod.rs
@@ -153,9 +153,19 @@ mod sip;
 /// Thankfully, you won't need to worry about upholding this property when
 /// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
 ///
+/// ## Prefix collisions
+///
+/// Implementations of `hash` should ensure that the data they
+/// pass to the `Hasher` are prefix-free. That is, different concatenations
+/// of the same data should not produce the same output.
+/// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra
+/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a",
+/// "bc")` hash differently.
+///
 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
 /// [`HashSet`]: ../../std/collections/struct.HashSet.html
 /// [`hash`]: Hash::hash
+/// [impl]: ../../std/primitive.str.html#impl-Hash
 #[stable(feature = "rust1", since = "1.0.0")]
 #[rustc_diagnostic_item = "Hash"]
 pub trait Hash {