#tree-structure #node-tree #tree #tree-node #index #slab


A vec-backed tree structure with tree-specific generational indexes

9 releases

0.3.2 May 2, 2020
0.3.1 Apr 7, 2020
0.3.0 Oct 24, 2019
0.2.0 Aug 24, 2019
0.1.2 Oct 7, 2018

#848 in Data structures

Download history 782/week @ 2024-03-14 827/week @ 2024-03-21 659/week @ 2024-03-28 611/week @ 2024-04-04 617/week @ 2024-04-11 691/week @ 2024-04-18 606/week @ 2024-04-25 540/week @ 2024-05-02 620/week @ 2024-05-09 542/week @ 2024-05-16 569/week @ 2024-05-23 662/week @ 2024-05-30 671/week @ 2024-06-06 471/week @ 2024-06-13 418/week @ 2024-06-20 316/week @ 2024-06-27

1,987 downloads per month
Used in 7 crates (4 directly)

MIT license



Build Status

A vec-backed tree structure with tree-specific generational indexes.


This library provides a Tree<T> struct which allows the creation and manipulation of in-memory trees. The tree itself is backed by a vector and the tree's node relationships are managed by tree-specific generational indexes called NodeIds (more below). In addition, "views" of tree nodes are handed out which are either immutable (NodeRef) or mutable (NodeMut) instead of handing out references directly. Most tree mutations are achieved by modifying NodeMuts instead of talking to the tree itself.

Trees in this crate are "just" trees. They do not allow cycles, and they do not allow arbitrary graph structures to be created. Each node in the tree can have an arbitrary number of children, and there is no weight associated with edges between the nodes in the tree.

Please Note: this crate does not support comparison-based data insertion. In other words, this is not a binary search tree (or any other kind of search tree) crate. It is purely a crate for storing data in a hierarchical manner. The caller must know the structure that they wish to build and then use this crate to do so; this library will not make those structural decisions for you.


This crate uses #![forbid(unsafe_code)] to prevent any and all unsafe code usage.

Example Usage

use slab_tree::*;

fn main() {

    //      "hello"
    //        / \
    // "world"   "trees"
    //              |
    //            "are"
    //              |
    //            "cool"

    let mut tree = TreeBuilder::new().with_root("hello").build();
    let root_id = tree.root_id().expect("root doesn't exist?");
    let mut hello = tree.get_mut(root_id).unwrap();



NodeIds are tree-specific generational indexes. Using generational indexes means that we can re-use space inside the tree (after nodes have been removed) without also having to re-use the same tree indexes which could potentially cause confusion and bugs. The "tree-specific" part means that indexes from one tree cannot be confused for indexes for another tree. This is because each index contains a process-unique-id which is shared by the tree from which that index originated.

Project Goals

  • Allow caller control of as many allocations as possible (through pre-allocation)
  • Fast and Ergonomic Node insertion and removal
  • Arbitrary Tree structure creation and manipulation


  • Arbitrary Graph structure creation and manipulation
  • Comparison-based node insertion of any kind