#dictionary #real #array #associative #exercise #key #dict-iface

dict

Exercise crate implementing real associative arrays, also known as dictionaries

5 releases

Uses old Rust 2015

0.1.5 Apr 17, 2018
0.1.4 Apr 17, 2018

#1207 in Rust patterns

Download history 218/week @ 2023-12-10 274/week @ 2023-12-17 121/week @ 2023-12-24 82/week @ 2023-12-31 226/week @ 2024-01-07 218/week @ 2024-01-14 189/week @ 2024-01-21 270/week @ 2024-01-28 223/week @ 2024-02-04 173/week @ 2024-02-11 191/week @ 2024-02-18 141/week @ 2024-02-25 473/week @ 2024-03-03 439/week @ 2024-03-10 210/week @ 2024-03-17 179/week @ 2024-03-24

1,318 downloads per month
Used in 5 crates

GPL-3.0 license

15KB

rust-dict

Crate implementing real associative arrays, also known as dictionaries.

use dict::{ Dict, DictIface };

//create a dictionary of strings
let mut dict = Dict::<String>::new();
assert_eq!( dict.is_empty(), true );
assert_eq!( dict.len(), 0 );

// add an element "val" indexed by the key "key"
assert_eq!( dict.add( "key".to_string(), "val".to_string() ), true );
assert_eq!( dict.is_empty(), false );
assert_eq!( dict.len(), 1 );

// keys must be unique
assert_eq!( dict.add( "key".to_string()      , "other_val".to_string() ), false );
assert_eq!( dict.len(), 1 );
assert_eq!( dict.add( "other_key".to_string(), "other_val".to_string() ), true );
assert_eq!( dict.len(), 2 );

// we can iterate just like an array
for o in &dict {
    println!( "{} - {}", o.key, o.val );
}
dict.iter().for_each( |o| println!( "{} - {}", o.key, o.val ) );

// we can access the value by its key string with get()
assert_eq!( dict.get( "key" ).unwrap(), "val" );
assert_eq!( dict.contains_key( "key" ), true );
assert_eq!( dict.remove_key( "key" ).unwrap(), "val" );
assert_eq!( dict.contains_key( "key" ), false );

ownyourbits.com

crates.io

No runtime deps