| Crates.io | flex |
| lib.rs | flex |
| version | 0.2.0 |
| created_at | 2026-01-02 20:44:53.176595+00 |
| updated_at | 2026-01-13 04:55:12.880333+00 |
| description | Flexible borrowing and ownership for Rust |
| homepage | https://github.com/npmccallum/flex |
| repository | https://github.com/npmccallum/flex |
| max_upload_size | |
| id | 2019172 |
| size | 36,676 |
Flexible borrowing and ownership for Rust.
Flex is an enum that holds either a borrowed reference or an owned boxed value
of the same type. It is similar in concept to Cow and works seamlessly with
unsized types like dyn Trait, [T], and str.
[dependencies]
flex = "0.1"
If your crate has an alloc feature, pass it through to flex:
[features]
alloc = ["flex/alloc"]
[dependencies]
flex = "0.1"
use flex::Flex;
// Start with a borrowed slice
let borrowed = Flex::Lend(&[1, 2, 3][..]);
assert_eq!(&*borrowed, &[1, 2, 3]);
# #[cfg(feature = "alloc")] {
// Or own a slice
let owned = Flex::Give(vec![4, 5, 6].into_boxed_slice());
assert_eq!(&*owned, &[4, 5, 6]);
// Convert borrowed to owned
let claimed: Flex<'static, [i32]> = borrowed.claim();
# }
While both Flex and Cow deal with borrowed vs owned data, they serve
different purposes:
&str/String, &[T]/Vec<T>)ToOwned trait for conversionalloc - not available in no_std&T vs Box<T>)dyn Trait, [u8] and strToOwned requirementno_std without alloc - produces consistent APIsExample: Flex<'a, str> holds either &'a str or Box<str>, while
Cow<'a, str> holds either &'a str or String.
Flex is particularly useful when:
Flex<'a, dyn Debug> holds &dyn Debug or
Box<dyn Debug>ToOwned constraintsalloc: Enables the Give variant with Box<T>Without alloc, Flex only supports the Lend variant, but APIs remain
compatible.
Licensed under the MIT License.