Expand description
A flexible, simple to use, immutable, clone-efficient String replacement for Rust
use flexstr::{flex_fmt, flex_str, FlexStr, IntoFlexStr, ToCase, ToFlexStr};
// Use `flex_str` macro to wrap literals as compile-time constants
const STATIC_STR: FlexStr = flex_str!("This will not allocate or copy");
assert!(STATIC_STR.is_static());
// 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!(inline_str.is_inlined());
// 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!(rc_str.is_heap());
// You can efficiently create a new `FlexStr` (without creating a `String`)
// This is equivalent to the stdlib `format!` macro
let inline_str2 = flex_fmt!("in{}", "lined");
assert!(inline_str2.is_inlined());
assert_eq!(inline_str, inline_str2);
// 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!(inline_str3.is_inlined());
assert_eq!(inline_str, inline_str3);
// Concatenation doesn't even copy if we can fit it in the inline string
let inline_str4 = inline_str3 + "!!!";
assert!(inline_str4.is_inlined());
assert_eq!(inline_str4, "inlined!!!");
// 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!(rc_str2.is_heap());
// Regardless of storage type, these all operate seamlessly together
// and choose storage as required
let heap_str2 = STATIC_STR + &inline_str;
assert!(heap_str2.is_heap());
assert_eq!(heap_str2, "This will not allocate or copyinlined"); Macros
AFlexStr equivalent to format! macro from stdlib. Efficiently creates a native AFlexStr
Create compile time constant AFlexStr (equivalent, but less typing than:
<AFlexStr>::from_static("my_literal")
Equivalent to a_flex_fmt except that it uses ufmt which is much faster, but has limitations.
See ufmt docs for more details
FlexStr equivalent to format! macro from stdlib. Efficiently creates a native FlexStr
Create compile time constant FlexStr (equivalent, but less typing than:
<FlexStr>::from_static("my_literal")
Structs
A flexible string type that transparently wraps a string literal, inline string, or a heap allocated type
Error type returned from try_to_static_str when this FlexStr does not contain a 'static str
Constants
Using this inline capacity will result in a type with the same memory size as a builtin String
Traits
A trait that converts the source to a AFlexStr while consuming the original
A trait that converts the source to a FlexStr<N, T> while consuming the original
A trait that converts the source to a FlexStr while consuming the original
Trait that can repeat a given FlexStr “n” times efficiently
A trait that converts the source to an AFlexStr without consuming it
Trait that provides uppercase/lowercase conversion functions for FlexStr
A trait that converts the source to a Flex<N, T> without consuming it
A trait that converts the source to a FlexStr without consuming it
Functions
FlexStr equivalent to format function from stdlib. Efficiently creates a native FlexStr