trie_hard_rs - Rust Implementation
A blazing fast, memory-efficient Trie (prefix tree) implementation for Rust with autocomplete support.
Part of the trie_hard family - high-performance Trie implementations across multiple programming languages.
Language Implementations
This repository contains Trie implementations for multiple languages, each optimized for its respective ecosystem:
More languages coming soon!
Performance
This Rust implementation delivers exceptional performance:
- Insert 10K words: 2.17ms
- Autocomplete (10 results): 18.4μs
- Lookup: 165μs
- Memory efficient with shared prefixes
- Unicode support with minimal overhead
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic usage:
use Trie;
let mut trie = new;
// Insert words with associated values
trie.insert;
trie.insert;
trie.insert;
// Fast lookups
assert_eq!;
// Prefix search
assert!;
// Autocomplete
let suggestions = trie.auto_complete;
// Returns: ["cat", "car", "card"]
Features
- Generic values: Store any
Clonetype as values - Batch operations: Efficient
add_word_listwith value generators - Unicode support: Full UTF-8 character support
- Memory efficient: Shared prefix storage
- Fast autocomplete: Configurable result limits
- Comprehensive tests: 37 test cases covering edge cases
- Benchmarked: Proven performance characteristics
Benchmarks
Run benchmarks with:
View detailed HTML reports at target/criterion/report/index.html
Advanced Usage
Batch Insertion with Value Generation
let words = ;
trie.add_word_list;
// Inserts with word length as value
Custom Value Types
let mut trie: = new;
trie.insert;
Autocomplete with Limits
// Get up to 5 suggestions
let suggestions = trie.auto_complete;
// Get all suggestions (no limit)
let all_suggestions = trie.auto_complete;
Working with Different Value Types
// String values
let mut string_trie = new;
string_trie.insert;
// Numeric values for scoring/ranking
let mut scored_trie = new;
scored_trie.insert;
scored_trie.insert;
// Complex data structures
let mut data_trie = new;
data_trie.insert;
API Reference
Core Methods
new()- Create a new empty Trieinsert(key, value)- Insert a key-value pairget(key)- Get value by exact key matchdelete(key)- Remove a key and its valueprefix_search(prefix)- Check if any words start with prefixauto_complete(prefix, max_results)- Get words starting with prefix
Batch Operations
add_word_list(words, value_generator)- Insert multiple words with generated values
Performance Characteristics
-
Time Complexity:
- Insert: O(k) where k = key length
- Lookup: O(k) where k = key length
- Autocomplete: O(k + m) where k = prefix length, m = results returned
- Delete: O(k) where k = key length
-
Space Complexity: O(ALPHABET_SIZE * N * M) where N = number of nodes, M = average key length
- Efficient prefix sharing reduces actual memory usage significantly
Cross-Language Compatibility
While each implementation is optimized for its language, they share:
- Consistent API design across all languages
- Similar performance characteristics
- Compatible data formats for cross-language projects
- Shared test cases to ensure behavioral consistency
Why trie_hard?
The trie_hard family was created to provide:
- Performance-first design - Optimized for speed and memory efficiency
- Production ready - Comprehensive testing and benchmarking
- Cross-language consistency - Same API patterns across implementations
- Developer friendly - Clear documentation and examples
- Unicode support - Works with international text out of the box
Comparison with Other Rust Trie Crates
| Feature | trie_hard | Other Crates |
|---|---|---|
| Autocomplete with limits | ✅ | ❌ |
| Generic value types | ✅ | Limited |
| Comprehensive benchmarks | ✅ | Limited |
| Unicode support | ✅ | Varies |
| Batch operations | ✅ | ❌ |
| Sub-microsecond autocomplete | ✅ | ❌ |
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request to the main repository.
Development
# Run tests
# Run benchmarks
# Check formatting
# Run lints
Related Projects
- Main trie_hard repository - All language implementations
- Go implementation