Crates.io | simple_dic |
lib.rs | simple_dic |
version | 0.1.4 |
source | src |
created_at | 2023-08-14 17:24:15.891549 |
updated_at | 2023-08-25 14:10:22.291681 |
description | This crate provides a simple dictionary implementation in Rust with various functions to manipulate and interact with the dictionary data structure. The dictionary allows you to store key-value pairs where keys are of type String and values can be of any type T. |
homepage | |
repository | https://github.com/dorian3343/simple_dic |
max_upload_size | |
id | 944380 |
size | 11,757 |
This repository provides a simple dictionary implementation in Rust with various functions to manipulate and interact with the dictionary data structure. The dictionary allows you to store key-value pairs where keys are of type String and values can be of any type T.
use simple_dic::Dictionary;
This function creates a new instance of the Dictionary struct. The boolean paramater is a new and expiremental feature that adds the possibility to toggle of uniqueness. As it's not fully implemented, I suggest keeping it as true for now.
let mut my_dict = Dictionary::<T>::new(true);
Use this function to add a new key-value pair to the dictionary. If the key already exists, it returns an error message.
let key = "name".to_string();
let value = "John".to_string();
match my_dict.push(key, value) {
Ok(()) => println!("Key-value pair added successfully."),
Err(err) => println!("Error: {}", err),
}
This function removes the newest (last-added) key-value pair from the dictionary.
my_dict.pop();
Use this function to check if a key exists in the dictionary.
if my_dict.search("name".to_string()) {
println!("Key found!");
} else {
println!("Key not found.");
}
This function returns the number of key-value pairs in the dictionary.
let num_entries = my_dict.len();
println!("Number of entries in the dictionary: {}", num_entries);
This function deletes a key-value pair based on the provided key. It returns true if the key-value pair was found and deleted, otherwise false.
if my_dict.drop("name".to_string()) {
println!("Key-value pair deleted successfully.");
} else {
println!("Key-value pair not found.");
}
This function checks if a given value exists in the dictionary's values.
if my_dict.contains(&"John".to_string()) {
println!("Value found in the dictionary.");
} else {
println!("Value not found.");
}
###index_of(&self,key: String) -> Result<Vec
let key = "search_key";
match obj.index_of(key.to_string()) {
Ok(indices) => {
println!("Indices of '{}': {:?}", key, indices);
}
Err(error) => {
println!("Error: {}", error);
}
}
This function overwrites the value associated with a key. If the key is found, the value is updated. If the key is not found, an error is returned. Not update for unique = false yet.
match my_dict.overwrite("name".to_string(), "Jane".to_string()) {
Ok(()) => println!("Value updated successfully."),
Err(err) => println!("Error: {}", err),
}
use simple_dic::Dictionary;
fn main() {
let mut my_dict = Dictionary::new(true);
my_dict.push("name".to_string(), "John".to_string()).unwrap();
my_dict.push("age".to_string(), 25.to_string()).unwrap();
println!("Dictionary contains 'name': {}", my_dict.search("name".to_string()));
println!("Dictionary length: {}", my_dict.len());
my_dict.pop();
println!("Dictionary length after pop: {}", my_dict.len());
my_dict.drop("age".to_string());
println!("Dictionary length after drop: {}", my_dict.len());
}