Enum Cow
enum Cow<'a, B>
where
B: ToOwned + ?Sized + 'a
A clone-on-write smart pointer.
The type Cow is a smart pointer providing clone-on-write functionality: it
can enclose and provide immutable access to borrowed data, and clone the
data lazily when mutation or ownership is required. The type is designed to
work with general borrowed data via the Borrow trait.
Cow implements Deref, which means that you can call
non-mutating methods directly on the data it encloses. If mutation
is desired, to_mut will obtain a mutable reference to an owned
value, cloning if necessary.
If you need reference-counting pointers, note that
[Rc::make_mut][crate::rc::Rc::make_mut] and
[Arc::make_mut][crate::sync::Arc::make_mut] can provide clone-on-write
functionality as well.
Examples
use Cow;
// No clone occurs because `input` doesn't need to be mutated.
let slice = ;
let mut input = from;
abs_all;
// Clone occurs because `input` needs to be mutated.
let slice = ;
let mut input = from;
abs_all;
// No clone occurs because `input` is already owned.
let mut input = from;
abs_all;
Another example showing how to keep Cow in a struct:
use Cow;
// Creates a container from borrowed values of a slice
let readonly = ;
let borrowed = new;
match borrowed
let mut clone_on_write = borrowed;
// Mutates the data from slice into owned vec and pushes a new value on top
clone_on_write.values.to_mut.push;
println!;
// The data was mutated. Let's check it out.
match clone_on_write
Variants
-
Borrowed(&'a B) Borrowed data.
-
Owned(<B as ToOwned>::Owned) Owned data.
Implementations
impl<B: ?Sized + ToOwned> Cow<'_, B>
const fn is_borrowed(c: &Self) -> boolReturns true if the data is borrowed, i.e. if
to_mutwould require additional work.Note: this is an associated function, which means that you have to call it as
Cow::is_borrowed(&c)instead ofc.is_borrowed(). This is so that there is no conflict with a method on the inner type.Examples
use Cow; let cow = Borrowed; assert!; let bull: = Owned; assert!;const fn is_owned(c: &Self) -> boolReturns true if the data is owned, i.e. if
to_mutwould be a no-op.Note: this is an associated function, which means that you have to call it as
Cow::is_owned(&c)instead ofc.is_owned(). This is so that there is no conflict with a method on the inner type.Examples
use Cow; let cow: = Owned; assert!; let bull = Borrowed; assert!;fn to_mut(self: &mut Self) -> &mut <B as ToOwned>::OwnedAcquires a mutable reference to the owned form of the data.
Clones the data if it is not already owned.
Examples
use Cow; let mut cow = Borrowed; cow.to_mut.make_ascii_uppercase; assert_eq!;fn into_owned(self: Self) -> <B as ToOwned>::OwnedExtracts the owned data.
Clones the data if it is not already owned.
Examples
Calling
into_ownedon aCow::Borrowedreturns a clone of the borrowed data:use Cow; let s = "Hello world!"; let cow = Borrowed; assert_eq!;Calling
into_ownedon aCow::Ownedreturns the owned data. The data is moved out of theCowwithout being cloned.use Cow; let s = "Hello world!"; let cow: = Owned; assert_eq!;
impl DerefPure for Cow<'_, str>
impl PartialEq for crate::borrow::Cow<'_, core::ffi::CStr>
fn eq(self: &Self, other: &CString) -> boolfn ne(self: &Self, other: &CString) -> bool
impl PartialEq for crate::borrow::Cow<'_, core::ffi::CStr>
fn eq(self: &Self, other: &&CStr) -> boolfn ne(self: &Self, other: &&CStr) -> bool
impl PartialEq for crate::borrow::Cow<'_, core::ffi::CStr>
fn eq(self: &Self, other: &CStr) -> boolfn ne(self: &Self, other: &CStr) -> bool
impl<'a> Add for Cow<'a, str>
fn add(self: Self, rhs: &'a str) -> <Self as >::Output
impl<'a> Add for Cow<'a, str>
fn add(self: Self, rhs: Cow<'a, str>) -> <Self as >::Output
impl<'a> AddAssign for Cow<'a, str>
fn add_assign(self: &mut Self, rhs: &'a str)
impl<'a> AddAssign for Cow<'a, str>
fn add_assign(self: &mut Self, rhs: Cow<'a, str>)
impl<'a> From for crate::borrow::Cow<'a, ByteStr>
fn from(s: &'a ByteString) -> Self
impl<'a> From for crate::borrow::Cow<'a, ByteStr>
fn from(s: &'a ByteStr) -> Self
impl<'a> From for crate::borrow::Cow<'a, ByteStr>
fn from(s: ByteString) -> Self
impl<'a> From for crate::borrow::Cow<'a, core::ffi::CStr>
impl<'a> From for crate::borrow::Cow<'a, core::ffi::CStr>
impl<'a> From for crate::borrow::Cow<'a, core::ffi::CStr>
impl<'a> From for crate::borrow::Cow<'a, str>
fn from(s: &'a str) -> Cow<'a, str>Converts a string slice into a
Borrowedvariant. No heap allocation is performed, and the string is not copied.Example
# use Cow; assert_eq!;
impl<'a> From for crate::borrow::Cow<'a, str>
impl<'a> From for crate::borrow::Cow<'a, str>
impl<'a> FromIterator for crate::borrow::Cow<'a, str>
fn from_iter<T: IntoIterator<Item = core::ascii::Char>>(it: T) -> Self
impl<'a> FromIterator for crate::borrow::Cow<'a, str>
fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str>
impl<'a> FromIterator for crate::borrow::Cow<'a, str>
fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str>
impl<'a> PartialEq for crate::borrow::Cow<'_, ByteStr>
fn eq(self: &Self, other: &ByteString) -> bool
impl<'a> PartialEq for crate::borrow::Cow<'_, [u8]>
fn eq(self: &Self, other: &ByteString) -> bool
impl<'a> PartialEq for crate::borrow::Cow<'_, str>
fn eq(self: &Self, other: &ByteString) -> bool
impl<'a> PartialEq for crate::borrow::Cow<'a, ByteStr>
fn eq(self: &Self, other: &&'a ByteStr) -> bool
impl<'a> PartialEq for crate::borrow::Cow<'a, [u8]>
fn eq(self: &Self, other: &&'a ByteStr) -> bool
impl<'a> PartialEq for crate::borrow::Cow<'a, str>
fn eq(self: &Self, other: &&'a ByteStr) -> bool
impl<'a> PartialOrd for crate::borrow::Cow<'_, ByteStr>
fn partial_cmp(self: &Self, other: &ByteString) -> Option<Ordering>
impl<'a> PartialOrd for crate::borrow::Cow<'_, [u8]>
fn partial_cmp(self: &Self, other: &ByteString) -> Option<Ordering>
impl<'a> PartialOrd for crate::borrow::Cow<'_, str>
fn partial_cmp(self: &Self, other: &ByteString) -> Option<Ordering>
impl<'a> PartialOrd for crate::borrow::Cow<'a, ByteStr>
fn partial_cmp(self: &Self, other: &&'a ByteStr) -> Option<Ordering>
impl<'a> PartialOrd for crate::borrow::Cow<'a, [u8]>
fn partial_cmp(self: &Self, other: &&'a ByteStr) -> Option<Ordering>
impl<'a> PartialOrd for crate::borrow::Cow<'a, str>
fn partial_cmp(self: &Self, other: &&'a ByteStr) -> Option<Ordering>
impl<'a, 'b> FromIterator for crate::borrow::Cow<'a, str>
fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str>
impl<'a, 'b> PartialEq for crate::borrow::Cow<'a, str>
fn eq(self: &Self, other: &String) -> boolfn ne(self: &Self, other: &String) -> bool
impl<'a, 'b> PartialEq for crate::borrow::Cow<'a, str>
fn eq(self: &Self, other: &&'b str) -> boolfn ne(self: &Self, other: &&'b str) -> bool
impl<'a, 'b> PartialEq for crate::borrow::Cow<'a, str>
fn eq(self: &Self, other: &str) -> boolfn ne(self: &Self, other: &str) -> bool
impl<'a, 'b, B, C> PartialEq for Cow<'a, B>
fn eq(self: &Self, other: &Cow<'b, C>) -> bool
impl<'a, B> Freeze for Cow<'a, B>
impl<'a, B> PartialOrd for Cow<'a, B>
fn partial_cmp(self: &Self, other: &Cow<'a, B>) -> Option<Ordering>
impl<'a, B> RefUnwindSafe for Cow<'a, B>
impl<'a, B> Send for Cow<'a, B>
impl<'a, B> Sync for Cow<'a, B>
impl<'a, B> Unpin for Cow<'a, B>
impl<'a, B> UnwindSafe for Cow<'a, B>
impl<'a, B: ?Sized + ToOwned> Borrow for Cow<'a, B>
fn borrow(self: &Self) -> &B
impl<'a, T> FromIterator for crate::borrow::Cow<'a, [T]>
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]>
impl<'a, T: Clone> From for crate::borrow::Cow<'a, [T]>
impl<'a, T: Clone> From for crate::borrow::Cow<'a, [T]>
impl<'a, T: Clone> From for crate::borrow::Cow<'a, [T]>
impl<'a, T: Clone, N: usize> From for crate::borrow::Cow<'a, [T]>
impl<B> Debug for Cow<'_, B>
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl<B> Default for Cow<'_, B>
fn default() -> SelfCreates an owned Cow<'a, B> with the default value for the contained owned value.
impl<B> Display for Cow<'_, B>
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl<B> Eq for Cow<'_, B>
impl<B> Hash for Cow<'_, B>
fn hash<H: Hasher>(self: &Self, state: &mut H)
impl<B> Ord for Cow<'_, B>
fn cmp(self: &Self, other: &Self) -> Ordering
impl<B: ?Sized + ToOwned> Clone for Cow<'_, B>
fn clone(self: &Self) -> Selffn clone_from(self: &mut Self, source: &Self)
impl<B: ?Sized + ToOwned> Deref for Cow<'_, B>
fn deref(self: &Self) -> &B
impl<P, T> Receiver for Cow<'a, B>
impl<T> Any for Cow<'a, B>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Cow<'a, B>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Cow<'a, B>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Cow<'a, B>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Cow<'a, B>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Cow<'a, B>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for Cow<'a, B>
fn to_string(self: &Self) -> String
impl<T, U> Into for Cow<'a, B>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> PartialEq for crate::borrow::Cow<'_, [T]>
fn eq(self: &Self, other: &&mut [U]) -> boolfn ne(self: &Self, other: &&mut [U]) -> bool
impl<T, U> PartialEq for crate::borrow::Cow<'_, [T]>
fn eq(self: &Self, other: &&[U]) -> boolfn ne(self: &Self, other: &&[U]) -> bool
impl<T, U> TryFrom for Cow<'a, B>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Cow<'a, B>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T, U, A: Allocator> PartialEq for crate::borrow::Cow<'_, [T]>
fn eq(self: &Self, other: &Vec<U, A>) -> boolfn ne(self: &Self, other: &Vec<U, A>) -> bool
impl<T: ?Sized + ToOwned> AsRef for Cow<'_, T>
fn as_ref(self: &Self) -> &T