A flexible, simple to use, immutable, clone-efficient String replacement for Rust
use ;
// Use `flex_str` macro to wrap literals as compile-time constants
const STATIC_STR: FlexStr = flex_str!;
assert!;
// Strings up to 22 bytes (on 64-bit) will be inlined automatically
// (demo only, use macro or `from_static` for literals as above)
let inline_str = "inlined".to_flex_str;
assert!;
// When a string is too long to be wrapped/inlined, it will heap allocate
// (demo only, use macro or `from_static` for literals as above)
let rc_str = "This is too long to be inlined".to_flex_str;
assert!;
// You can efficiently create a new `FlexStr` (without creating a `String`)
// This is equivalent to the stdlib `format!` macro
let inline_str2 = flex_fmt!;
assert!;
assert_eq!;
// We can upper/lowercase strings without converting to a `String` first
// This doesn't heap allocate since inlined
let inline_str3: FlexStr = "INLINED".to_ascii_lower;
assert!;
assert_eq!;
// Concatenation doesn't even copy if we can fit it in the inline string
let inline_str4 = inline_str3 + "!!!";
assert!;
assert_eq!;
// Clone is cheap, and never allocates
// (at most it is a ref count increment for heap allocated strings)
let rc_str2 = rc_str.clone;
assert!;
// Regardless of storage type, these all operate seamlessly together
// and choose storage as required
let heap_str2 = STATIC_STR + &inline_str;
assert!;
assert_eq!;