@@ -101,6 +101,25 @@ use crate::ops::{BitAnd, BitOr, BitXor, Sub};
101101/// [`HashMap`]: crate::collections::HashMap
102102/// [`RefCell`]: crate::cell::RefCell
103103/// [`Cell`]: crate::cell::Cell
104+ ///
105+ /// # Usage in `const` and `static`
106+ ///
107+ /// Like `HashMap`, `HashSet` is randomly seeded: each `HashSet` instance uses a different seed,
108+ /// which means that `HashSet::new` cannot be used in const context. To construct a `HashSet` in the
109+ /// initializer of a `const` or `static` item, you will have to use a different hasher that does not
110+ /// involve a random seed, as demonstrated in the following example. **`HashSet` constructed this
111+ /// way are not resistant against HashDoS!**
112+ ///
113+ /// ```rust
114+ /// use std::collections::HashSet;
115+ /// use std::hash::{BuildHasherDefault, DefaultHasher};
116+ /// use std::sync::Mutex;
117+ ///
118+ /// const EMPTY_SET: HashSet<String, BuildHasherDefault<DefaultHasher>> =
119+ /// HashSet::with_hasher(BuildHasherDefault::new());
120+ /// static SET: Mutex<HashSet<String, BuildHasherDefault<DefaultHasher>>> =
121+ /// Mutex::new(HashSet::with_hasher(BuildHasherDefault::new()));
122+ /// ```
104123#[ cfg_attr( not( test) , rustc_diagnostic_item = "HashSet" ) ]
105124#[ stable( feature = "rust1" , since = "1.0.0" ) ]
106125pub struct HashSet < T , S = RandomState > {
@@ -369,7 +388,10 @@ impl<T, S> HashSet<T, S> {
369388 /// ```
370389 #[ inline]
371390 #[ stable( feature = "hashmap_build_hasher" , since = "1.7.0" ) ]
372- #[ rustc_const_unstable( feature = "const_collections_with_hasher" , issue = "102575" ) ]
391+ #[ rustc_const_stable(
392+ feature = "const_collections_with_hasher" ,
393+ since = "CURRENT_RUSTC_VERSION"
394+ ) ]
373395 pub const fn with_hasher ( hasher : S ) -> HashSet < T , S > {
374396 HashSet { base : base:: HashSet :: with_hasher ( hasher) }
375397 }
0 commit comments