Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nachoparker/rust-dict
Exercise crate implementing real associative arrays, also known as dictionaries.
https://github.com/nachoparker/rust-dict
associative-array dictionary rust rust-lang
Last synced: about 1 month ago
JSON representation
Exercise crate implementing real associative arrays, also known as dictionaries.
- Host: GitHub
- URL: https://github.com/nachoparker/rust-dict
- Owner: nachoparker
- License: gpl-3.0
- Created: 2018-04-16T20:07:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-23T14:25:02.000Z (about 3 years ago)
- Last Synced: 2024-10-13T03:19:12.414Z (3 months ago)
- Topics: associative-array, dictionary, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 13.7 KB
- Stars: 2
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-dict
Exercise crate implementing real associative arrays, also known as dictionaries.This is a toy example. In practice better use std::collections::HashMap
```
use dict::{ Dict, DictIface };//create a dictionary of strings
let mut dict = Dict::::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](https://ownyourbits.com)
[crates.io](https://crates.io/crates/dict)