hset

Macro hset 

Source
macro_rules! hset {
    (
  $( $key: expr ),* $( , )?
 ) => { ... };
}
Expand description

Function and structures to work with collections. Creates a HashSet from a list of elements.

The hset macro allows for convenient creation of a HashSet with initial elements.

§Origin

This collection can be reexported from different crates :

  • from std, if use_std is on ( no_std flag if off )
  • from hashbrown, if use_alloc flag if on

§Syntax

The macro can be called with a comma-separated list of elements. A trailing comma is optional.

// HashSet of &str
let set1 = hset!( "a", "b", "c" );

// HashSet of &str
let set2 = hset!{ "a", "b", "c" };

// With trailing comma
let set3 = hset!( 1, 2, 3, );

§Parameters

  • $( $key: expr ),* $( , )? : A comma-separated list of elements to insert into the HashSet. Each element can be of any type that implements the Into< T > trait, where T is the type stored in the HashSet.

§Returns

Returns a HashSet containing all the specified elements. The capacity of the set is automatically determined based on the number of elements provided.

§Example

Basic usage with string slices :

let set = hset!( "one", "two", "three" );
assert!( set.contains( "one" ) );
assert!( set.contains( "two" ) );
assert!( set.contains( "three" ) );
assert_eq!( set.len(), 3 );

§Example

Creating a HashSet of &str from string literals :

let s = hset!{ "value" };
assert_eq!( s.get( "value" ), Some( &"value" ) );