Enum std::borrow::CowStable
[-] [+]
[src]
pub enum Cow<'a, B> where B: 'a, B: ToOwned, B: ?Sized {
Borrowed(&'a B),
Owned(<B as ToOwned>::Owned),
}
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 both 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 references to an owned
value, cloning if necessary.
Example
fn main() { use std::borrow::Cow; fn abs_all(input: &mut Cow<[int]>) { for i in 0..input.len() { let v = input[i]; if v < 0 { // clones into a vector the first time (if not already owned) input.to_mut()[i] = -v; } } } }use std::borrow::Cow; fn abs_all(input: &mut Cow<[int]>) { for i in 0..input.len() { let v = input[i]; if v < 0 { // clones into a vector the first time (if not already owned) input.to_mut()[i] = -v; } } }
Variants
Borrowed | Borrowed data. |
Owned | Owned data. |
Methods
impl<'a, B> Cow<'a, B> where B: ToOwned, B: ?Sized
fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned
Acquire a mutable reference to the owned form of the data.
Copies the data if it is not already owned.
fn into_owned(self) -> <B as ToOwned>::Owned
Extract the owned data.
Copies the data if it is not already owned.
fn is_borrowed(&self) -> bool
Returns true if this Cow
wraps a borrowed value
fn is_owned(&self) -> bool
Returns true if this Cow
wraps an owned value