10 releases
Uses old Rust 2015
0.3.10 | Aug 3, 2018 |
---|---|
0.3.9 | Jun 8, 2018 |
0.2.8 | May 27, 2018 |
0.2.6 | Mar 15, 2018 |
0.1.4 | Mar 14, 2018 |
#2055 in Data structures
25KB
317 lines
ident
A Rust utility crate for wrapping types with an immutable identifier and storing/accessing such types in collections by that identifier.
How Do I Use This Crate?
First add the crate to your Cargo.toml
:
[dependencies]
ident = "*" # or the specific version you want to use.
And import the crate to your own main.rs
/lib.rs
:
extern crate ident;
use ident::*;
Ok, But What Does This Crate Do?
Lets say you have some type:
#[derive(Clone)]
struct Foo {
x: usize
}
impl Foo {
pub fn new(x: usize) -> Self {
Self { x }
}
pub fn do_stuff(&mut self) {
//Your code.
}
}
And you have a collection of Foo
s
use std::collections::HashMap;
fn main() {
let mut my_foos = HashMap::with_capacity(2);
my_foos.insert(5, Foo::new(10));
my_foos.insert(10, Foo::new(5));
let mut foo = my_foos.get(&5).unwrap().clone();
foo.do_stuff();
}
Its often useful to remember where you got you value from (my_foos[5]
in this case). That would normally mean creating a new variable which you have to remember to pass everywhere but with ident
:
use ident::*;
use std::collections::HashMap;
fn main() {
let mut my_foos = HashMap::with_capacity(2);
my_foos.insert(5, Foo::new(10));
my_foos.insert(10, Foo::new(5));
let mut foo = WithIdent::new(5, my_foos.get(5).unwrap().clone());
foo.do_stuff();
}
We are able to get the key bundled with the value while still accessing the value as if the key wasn't there.
This is a simple use case however:
- Getting and Inserting with an "identifier" is implemented on standard collections.
- Rust is able to infer the type of your value without your intervention.
- There are several utility functions for
WithIdent
which allow you to manipulate the inner value or the identifier as needed.