Crates.io | ego-tree |
lib.rs | ego-tree |
version | 0.10.0 |
source | src |
created_at | 2016-01-10 23:26:13.386552 |
updated_at | 2024-12-09 23:52:19.427031 |
description | Vec-backed ID-tree |
homepage | |
repository | https://github.com/rust-scraper/ego-tree |
max_upload_size | |
id | 3866 |
size | 94,922 |
ego-tree
is a Rust crate that provides a Vec-backed ID-tree implementation. It offers a flexible and efficient way to create and manipulate tree structures in Rust, with a focus on performance and ease of use.
ego-tree
is on Crates.io and GitHub.
The design of ego-tree
is centered around the following principles:
Efficiency: The tree structure is backed by a Vec, allowing for fast, cache-friendly operations and efficient memory usage.
Flexibility: Nodes can have any number of children, allowing for the representation of various tree structures.
Stability: Node references remain valid even after modifying the tree structure, thanks to the use of stable NodeId indices.
Safety: The API is designed to prevent common errors, such as creating cycles or detaching the root node.
Ergonomics: The crate provides both low-level operations and high-level conveniences like the tree!
macro for easy tree construction.
Unlike traditional pointer-based trees, ego-tree
uses a Vec to store all nodes. This design choice offers several advantages:
Nodes are identified by NodeId
s, which are wrappers around indices into the underlying Vec. This approach allows for:
The crate provides both NodeRef
(immutable) and NodeMut
(mutable) types for working with nodes. This separation allows for:
Nodes can be detached from the tree but not removed entirely. This design choice:
The crate provides a variety of iterator types for traversing the tree in different ways. This design:
ego-tree
is well-suited for applications that require:
Some potential use cases include:
Add this to your Cargo.toml
:
[dependencies]
ego-tree = "0.6.2"
Basic usage:
use ego_tree::Tree;
let mut tree = Tree::new(1);
let mut root = tree.root_mut();
root.append(2);
let mut child = root.append(3);
child.append(4);
child.append(5);
For more detailed usage examples and API documentation, please refer to the documentation.
This project is licensed under the ISC License.
Contributions are welcome! Please feel free to submit a Pull Request.
ego-tree
is created and maintained by the team of rust-scraper.