Enum std::str::MaybeOwnedDeprecated
[-]
[+]
[src]
pub enum MaybeOwned<'a> { Slice(&'a str), Owned(String), }
A string type that can hold either a String
or a &str
.
This can be useful as an optimization when an allocation is sometimes
needed but not always.
Variants
Slice | A borrowed string. |
Owned | An owned string. |
Methods
impl<'a> MaybeOwned<'a>
fn is_owned(&self) -> bool
Returns true
if this MaybeOwned
wraps an owned string.
Examples
fn main() { let string = String::from_str("orange"); let maybe_owned_string = string.into_maybe_owned(); assert_eq!(true, maybe_owned_string.is_owned()); }let string = String::from_str("orange"); let maybe_owned_string = string.into_maybe_owned(); assert_eq!(true, maybe_owned_string.is_owned());
fn is_slice(&self) -> bool
Returns true
if this MaybeOwned
wraps a borrowed string.
Examples
fn main() { let string = "orange"; let maybe_owned_string = string.as_slice().into_maybe_owned(); assert_eq!(true, maybe_owned_string.is_slice()); }let string = "orange"; let maybe_owned_string = string.as_slice().into_maybe_owned(); assert_eq!(true, maybe_owned_string.is_slice());
fn len(&self) -> uint
Return the number of bytes in this string.
fn is_empty(&self) -> bool
Returns true if the string contains no bytes
Trait Implementations
impl<'a> BytesContainer for MaybeOwned<'a>
fn container_as_bytes<'b>(&'b self) -> &'b [u8]
fn container_as_str<'b>(&'b self) -> Option<&'b str>
fn is_str(_: Option<&MaybeOwned>) -> bool
impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a>
fn into_maybe_owned(self) -> MaybeOwned<'a>
Examples
fn main() { let str = "orange"; let maybe_owned_str = str.as_slice().into_maybe_owned(); let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned(); assert_eq!(false, maybe_maybe_owned_str.is_owned()); }let str = "orange"; let maybe_owned_str = str.as_slice().into_maybe_owned(); let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned(); assert_eq!(false, maybe_maybe_owned_str.is_owned());