bmap

Macro bmap 

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

Function and structures to work with collections. Creates a BTreeMap from a list of key-value pairs.

The bmap macro facilitates the convenient creation of a BTreeMap with initial elements.

§Origin

This collection is reexported from alloc.

§Syntax

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

// BTreeMap of &str to i32
let map1 = bmap!( "one" => 1, "two" => 2, "three" => 3 );

// BTreeMap of &str to &str
let map2 = bmap!{ "name" => "value" };

// With trailing comma
let map3 = bmap!( 1 => "one", 2 => "two", 3 => "three", );

§Parameters

  • $( $key: expr => $value: expr ),* $( , )? : A comma-separated list of key-value pairs to insert into the BTreeMap. Each key and value can be of any type that implements the Into< K > and Into< V > traits, where K and V are the types stored in the BTreeMap as keys and values, respectively.

§Returns

Returns a BTreeMap containing all the specified key-value pairs. The map’s capacity is automatically determined based on the number of elements provided.

§Example

Basic usage with string slices and integer values :

let map = bmap!( "one" => 1, "two" => 2, "three" => 3 );
assert_eq!( map.get( "one" ), Some( &1 ) );
assert_eq!( map.get( "two" ), Some( &2 ) );
assert_eq!( map.get( "three" ), Some( &3 ) );

§Example

Creating a BTreeMap of integers to string slices from literals :

let numbers = bmap!( 1 => "one", 2 => "two", 3 => "three" );
assert_eq!( numbers.get( &1 ), Some( &"one" ) );
assert_eq!( numbers.get( &2 ), Some( &"two" ) );
assert_eq!( numbers.get( &3 ), Some( &"three" ) );