pub enum Str {
Static(&'static str),
Rc(Arc<String>),
Owned(String),
}Expand description
A string type that can hold either a 'static string slice, an
Arc<String> for cheap cloning, or an owned String.
This enum is useful when you need to store string data that may be static, shared, or owned without incurring unnecessary allocations.
§Variants
Static(&'static str)– A reference to a compile‑time constant string.Rc(Arc<String>)– A reference‑counted pointer to aString, allowing cheap cloning of the underlying data.Owned(String)– An owned heap‑allocatedString.
Variants§
Static(&'static str)
A reference to a compile‑time constant string.
Rc(Arc<String>)
A reference‑counted pointer to a String.
Owned(String)
An owned heap‑allocated String.
Implementations§
Source§impl Str
impl Str
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns a string slice (&str) that references the data inside the
Str, regardless of which variant it holds.
§Examples
use anyval::Str;
let s = Str::from_static("hello");
assert_eq!(s.as_str(), "hello");Sourcepub fn from_static(s: &'static str) -> Self
pub fn from_static(s: &'static str) -> Self
Constructs a Str from a 'static string slice.
This does not allocate; the variant stored is Str::Static.
§Examples
use anyval::Str;
let s = Str::from_static("static");
assert!(s.is_static());Sourcepub fn from_owned(s: String) -> Self
pub fn from_owned(s: String) -> Self
Constructs a Str from an owned String.
The String is moved into the Str::Owned variant.
§Examples
use anyval::Str;
let original = String::from("owned");
let s = Str::from_owned(original);
// `original` can no longer be used hereSourcepub fn from_rc(s: Arc<String>) -> Self
pub fn from_rc(s: Arc<String>) -> Self
Constructs a Str from an Arc<String>.
The Arc is moved into the Str::Rc variant, allowing cheap cloning.
§Examples
use std::sync::Arc;
use anyval::Str;
let arc = Arc::new(String::from("shared"));
let s = Str::from_rc(arc.clone());
assert!(s.is_rc());Sourcepub fn take(self) -> String
pub fn take(self) -> String
Consumes the Str and returns an owned String.
- For
Str::Static, the static slice is cloned into a newString. - For
Str::Rc, the underlyingStringis cloned. - For
Str::Owned, the innerStringis returned without allocation.
§Examples
use anyval::Str;
let s = Str::from_owned(String::from("owned"));
let owned = s.take(); // `s` is moved
assert_eq!(owned, "owned");Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Alias for [take]; consumes the Str and returns an owned String.
This method exists for API symmetry with other types that provide an
into_* conversion.
Sourcepub fn is_static(&self) -> bool
pub fn is_static(&self) -> bool
Returns true if the Str is the Static variant.
§Examples
use anyval::Str;
let s = Str::from_static("static");
assert!(s.is_static());