README.md
1# slotmap
2
3A Rust library providing three containers with persistent unique keys to access
4stored values, `SlotMap`, `HopSlotMap` and `DenseSlotMap`. Upon insertion a key
5is returned that can be used to later access or remove the values. Insertion,
6deletion and access all take O(1) time with low overhead. Great for storing
7collections of objects that need stable, safe references but have no clear
8ownership otherwise, such as game entities or graph nodes. Two secondary maps,
9`SecondaryMap` and `SparseSecondaryMap` are also provided that allow you to map
10further objects to the keys created by one of the slot maps. Please refer to
11[**the documentation**](https://docs.rs/slotmap) for more information.
12
13The minimum required stable Rust version for `slotmap` is 1.49. To start using
14`slotmap` add the following to your `Cargo.toml`:
15
16```toml
17[dependencies]
18slotmap = "1.0"
19```
20
21# Example
22
23A short example:
24
25```rust
26use slotmap::{SlotMap, SecondaryMap};
27
28let mut sm = SlotMap::new();
29let foo = sm.insert("foo"); // Key generated on insert.
30let bar = sm.insert("bar");
31assert_eq!(sm[foo], "foo");
32assert_eq!(sm[bar], "bar");
33
34sm.remove(bar);
35let reuse = sm.insert("reuse"); // Space from bar reused.
36assert_eq!(sm.contains_key(bar), false); // After deletion a key stays invalid.
37
38let mut sec = SecondaryMap::new();
39sec.insert(foo, "noun"); // We provide the key for secondary maps.
40sec.insert(reuse, "verb");
41
42for (key, val) in sm {
43 println!("{} is a {}", val, sec[key]);
44}
45```
46
47# License
48
49`slotmap` is released under the Zlib license, a permissive license. It is
50OSI and FSF approved and GPL compatible.
51