A Rust utility crate for wrapping types with an immutable identifier and storing/accessing such types in collections by that identifier.
First add the crate to your Cargo.toml
:
toml
[dependencies]
ident = "*" # or the specific version you want to use.
And import the crate to your own main.rs
/lib.rs
:
```rust
extern crate ident;
use ident::*; ```
Lets say you have some type: ```rust
stuct 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
rust
use std::collections::HashMap;
fn main() {
let mut myfoos = HashMap::
let mut foo = myfoos.get(&5).clone();
foo.dostuff();
}
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`:
rust
use ident::*;
use std::collections::HashMap;
fn main() {
let mut myfoos = HashMap::
let mut foo = WithIdent::map(myfoos.getwithident(5), Clone::clone);
foo.dostuff();
}
``
We are able to get the
keybundled the
value` while still accessing the value as if it wasn't there.
This is a simple use case however:
* Getting and Inserting with with an "identifier" is implemented on standard collections.
* Rusts 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.