FlatString
A FlatString is a string that is stored in a fixed-size / flat array of characters. This is useful for scenarios where you want to avoid dynamic memory allocation, or where you want to store a string in a fixed-size buffer. A FlatString is a simple struct that contains:
- a fixed-size array of type u8
- the length of the string
- the number of characters.
The maximum store capacity of the FlatString is limited to 255 bytes (so a string has to be less than 255 characters long). As such, the length of the string (in bytes) and the number of characters are stored as a u8 as well.
For example, a FlatString<14> will have a size of 16 bytes (14 bytes for the string, 1 byte for the length, and 1 byte for the number of characters).
Usage
To use a FlatString, you need to add the following to your Cargo.toml:
[]
= "1.0.0"
Then, you can use the FlatString as follows:
use FlatString;
Methods
The following methods are available for the FlatString:
| Method | Description |
|---|---|
from_str |
Create a FlatString from a string slice |
new |
Create an empty FlatString |
len |
Get the length of the string |
is_empty |
Check if the string is empty |
chars_count |
Get the number of characters in the string |
capacity |
Get the capacity of the string |
as_str |
Get the string as a &str |
clear |
Clear the string |
push_str |
Ads a string slice to the existig string. If the resulted string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
push |
Adds a character to the existing string. If the resulted string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
try_push_str |
Ads a string slice to the existig string only if the resulted string fits in the preallocated capacity. In this case this method will return Some(&str) otherwise it returns None |
try_push |
Adds a character to the existing string only if the resulted string fits in the preallocated capacity. In this case this method will return Some(&str) otherwise it returns None |
set |
Set the string to a new value. If the new string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
truncate |
Truncate the string to a specific number of bytes. |
pop |
Removes the last character from the string buffer and returns it. |
insert |
Inserts a string slice into this FlatString at a byte position. |
insert_char |
Inserts a character into this FlatString at a byte position. |
remove |
Removes a char from this FlatString at a byte position and returns it. |
FlatString implements the following traits:
std::fmt::Displayandstd::fmt::Debug(this allows you to print the string usingprintln!anddbg!)std::ops::Deref(this allows you to use the*operator to get the string as a&str). This will also allow you to use theFlatStringas a&strin function arguments.CopyandClone(this allows you to copy theFlatStringusing theCopytrait)PartialEqandEq(this allows you to compare twoFlatStringusing the==operator)PartialOrdandOrd(this allows you to compare twoFlatStringusing the<,>,<=, and>=operators)Default(this allows you to create an emptyFlatString)
Example
-
Create a
FlatStringfrom a string slice:let s = from_str; // s will be "Hello World !" -
Create a
FlatStringfrom a string slice that is larger than its capacity:let s = from_str; // s will be "Hello Worl" (truncated to fit 10 bytes) -
Add a
&strto an existingFlatString:let mut s = from_str; // s is "Hello" s.push_str; // s is "Hello World !" -
Add a
&strto an existingFlatStringand exceed the capacity:let mut s = from_str; // s is "Hello" s.push_str; // s is "Hello Wo" (truncated to fit 8 bytes) -
Add a
&strto an existingFlatStringusingtry_push_str:let mut s = new; if let Some = s.try_push_str else let result = s.try_push_str -
Validating the size of a
FlatStringlet mut s = new; assert_eq!; // the size of the FlatString is 10 bytes: // - 8 bytes for the string // - 1 byte for the length // - 1 byte for the number of characters