#dictionary #associative #array #real #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

#11 in #associative

Download history 178/week @ 2024-07-22 217/week @ 2024-07-29 167/week @ 2024-08-05 242/week @ 2024-08-12 110/week @ 2024-08-19 215/week @ 2024-08-26 132/week @ 2024-09-02 131/week @ 2024-09-09 123/week @ 2024-09-16 154/week @ 2024-09-23 120/week @ 2024-09-30 75/week @ 2024-10-07 112/week @ 2024-10-14 117/week @ 2024-10-21 113/week @ 2024-10-28 148/week @ 2024-11-04

493 downloads per month
Used in 4 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