Logo

Programming-Idioms

  • Rust
use std::collections::HashMap;
let x: HashMap<&str, i32> = [
    ("one", 1),
    ("two", 2),
].into_iter().collect();
use std::collections::BTreeMap;
let mut x = BTreeMap::new();
x.insert("one", 1);
x.insert("two", 2);

Something different than a BTreeMap might be used, depending on the usage.

The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.

Maps in Rust are generic and type safe at compile time.
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;
declare
   package Maps is new Indefinite_Hashed_Maps (Key_Type => String,
                                               Element_Type => Integer,
                                               Hash => Ada.Strings.Hash,
                                               Equivalent_Keys => "=");
      
   use Maps;
      
   X : Map := Empty_Map;
begin
   X.Insert ("One", 1);
   X.Insert ("Two", 2);
   X.Insert ("Three", 3);
end;

New implementation...