Yangon
A high-performance, stack-allocated string type for Rust with fixed capacity and zero heap allocations.
Overview
Yangon is a production-ready string library that provides a stack-allocated alternative to String. By using fixed-capacity storage and const generics, Yangon eliminates heap allocations for performance-critical applications while maintaining a familiar String-like API.
Performance Highlights:
- 25x faster than
std::string::Stringforpush_stroperations - Zero heap allocations - all data stored on the stack
- Configurable capacity via const generics
- Full UTF-8 validation and support
Installation
Add Yangon to your Cargo.toml:
[]
= "0.0.3"
Quick Start
use ;
// Create with default 10KB capacity
let mut s = new;
s.push_str.unwrap;
s.push_str.unwrap;
println!; // "Hello, Yangon!"
// Or use the macro
let s = yangon!;
// Custom capacity with const generics
let mut small: = with_capacity;
small.push_str.unwrap;
Key Features
Stack Allocation
- No heap allocations - all string data lives on the stack
- Predictable memory usage - capacity known at compile time
- Default 10KB capacity when using
Yangon::new()orYangon::from() - Configurable via const generics -
Yangon<N>where N is byte capacity
String-Like API
Yangon provides familiar methods similar to String:
let mut s = yangon!;
// Push operations
s.push_str.unwrap;
s.push.unwrap;
// Modification
s.insert;
s.remove;
s.pop;
s.clear;
s.truncate;
// Inspection
assert_eq!;
assert!;
assert_eq!;
Advanced Pattern Matching
Yangon's replace function supports multiple pattern types with turbo fish syntax:
let s = yangon!;
// Replace string slice
let s1 = s.;
// Replace single character
let s2 = s.;
// Replace multiple characters
let s3 = s.;
// Replace with closure (e.g., remove whitespace)
let s4 = s.;
UTF-8 Handling
Full UTF-8 support with validation:
use Yangon;
// From valid UTF-8
let s = from_utf8.unwrap;
// Unchecked (unsafe but faster)
let s = unsafe ;
// Lossy conversion (replaces invalid sequences with �)
let bytes = vec!;
let s = from_utf8_lossy;
Iteration and Collection
use Yangon;
// From iterator
let chars = vec!;
let s: Yangon = chars.into_iter.collect;
// Retain with predicate
let mut s = yangon!;
s.retain;
assert_eq!;
Additional Operations
let mut s = yangon!;
// Trimming (returns &str)
assert_eq!;
// Split off at index
let s2 = s.split_off;
// Replace range
s.replace_range;
// Convert to bytes
let bytes: = s.into_bytes;
// Convert to String
let string: String = s.to_string;
Use Cases
Yangon is ideal for:
- Performance-critical code where heap allocation overhead is unacceptable
- Embedded systems with limited or no heap allocation
- Network protocols requiring fixed-size string buffers
- Data transfer/storage with known maximum string lengths
- Real-time applications requiring predictable performance
Real-World Example
Currently used in production for a Facebook Reel downloader application, handling URL manipulation and data processing with consistent performance.
Performance Considerations
When Yangon Excels
push_stroperations: ~25x faster thanStringpushoperations: Significantly faster- Character-by-character building: Minimal allocation overhead
- Short to medium strings fitting in capacity
Performance Notes
replacefunction is slower thanString::replace- avoid in hot paths when possible- Fixed capacity means capacity overflow returns
Err(yError::CapacityOverflow) - Best for data transfer and storage; consider
Stringfor highly dynamic string manipulation
Safety
Yangon uses unsafe internally for performance-critical operations but maintains safety guarantees:
- UTF-8 validation on all public APIs (except
_uncheckedvariants) - Bounds checking prevents invalid memory access
- Production tested under stress conditions
- Users must respect capacity limits - overflows return errors or panic
_uncheckedvariants assume valid input for maximum performance
Responsibility: Yangon is not as flexible as String due to fixed capacity. Use with understanding of your data size requirements.
API Conversion Reference
| String Method | Yangon Equivalent | Returns |
|---|---|---|
String::new() |
Yangon::new() |
Yangon |
String::from(s) |
Yangon::from(s) |
Yangon |
s.push_str(s) |
s.push_str(s) |
Result<(), yError> |
s.push(c) |
s.push(c) |
Result<(), yError> |
s.as_str() |
s.as_str() |
&str |
String::from_utf8(v) |
Yangon::from_utf8(v) |
Result<Yangon, yError> |
String::from_utf8_lossy(v) |
Yangon::from_utf8_lossy(v) |
yCow<Yangon> |
Traits Implemented
Display- Format for printingDebug- Debug formattingWrite- Format writing supportDeref<Target = str>- Automatic coercion to&strAsRef<str>- Borrow as string slicePartialEq<&str>- Compare with string slicesFromIterator<char>- Build from character iteratorClone- Deep copy support
Error Handling
use yError;
match s.push_str
Macro Usage
The yangon! macro provides convenient initialization:
// Empty Yangon
let empty = yangon!;
// With initial content
let s = yangon!;
// Note: Only accepts 0 or 1 string literal
Documentation
Full API documentation is coming soon to docs.rs.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
This is a stable, production-ready project. While it's not actively seeking contributors, bug reports and suggestions are welcome via GitHub issues.
About the Name
Yangon is named after the capital city of Myanmar. The name reflects the project's foundation: solid, reliable, and built for real-world use.
Note: Yangon prioritizes performance over flexibility. Understand your string size requirements before choosing Yangon over String. When used appropriately, it provides significant performance benefits with zero heap allocation overhead.