pub enum Cow<'a, B> where B: 'a + ToOwned + ?Sized {
Borrowed(&'a B),
Owned(B::Owned),
}
1.0.0A 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.
use std::borrow::Cow;
fn abs_all(input: &mut Cow<[i32]>) {
for i in 0..input.len() {
let v = input[i];
if v < 0 {
input.to_mut()[i] = -v;
}
}
}
Variants
Methods
impl<'a, B> Cow<'a, B> where B: ToOwned + ?Sized
fn to_mut(&mut self) -> &mut B::Owned
Acquires a mutable reference to the owned form of the data.
Clones the data if it is not already owned.
use std::borrow::Cow;
let mut cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
let hello = cow.to_mut();
assert_eq!(hello, &[1, 2, 3]);
Extracts the owned data.
Clones the data if it is not already owned.
use std::borrow::Cow;
let cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
let hello = cow.into_owned();
assert_eq!(vec![1, 2, 3], hello);
Trait Implementations
type Target = B
fn deref(&self) -> &B
impl<'a, B> Eq for Cow<'a, B> where B: Eq + ToOwned + ?Sized
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
impl<'a, B> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned + ?Sized
impl<'a, T> AsRef<T> for Cow<'a, T> where T: ToOwned + ?Sized
impl<'a, T> From<Vec<T>> for Cow<'a, [T]> where T: Clone
1.7.0
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn ne(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
impl<'a> From<&'a Path> for Cow<'a, Path>
1.6.0
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn ne(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn ne(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
fn ne(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool