pub trait Cloned {
    type Cloned;

    fn cloned_(&self) -> Self::Cloned;
}
Available on crate feature collections only.
Expand description

Clones a collection of references into a collection of values.

Features

Enabling the “alloc” or “std” features changes the impl for references from using Clone bounds to using ToOwned.

Enabling the “rust_1_51” feature allows arrays of all lengths to implement this trait, otherwise it’s only implemented for arrays up to 32 elements long.

ToOwned is implemented for all types that implement Clone, and is not declared in the core crate, which means that you can’t call cloned_ on (&str,) or (&[T],) without enabling either the “alloc” or “std” features.

Examples

Tuples

use core_extensions::collections::Cloned;

assert_eq!((&2,).cloned_(), (2,));
assert_eq!((&2, &3).cloned_(), (2, 3));
assert_eq!((&2, &3, &5).cloned_(), (2, 3, 5));
assert_eq!((&2, &3, &5, &8).cloned_(), (2, 3, 5, 8));

Arrays

use core_extensions::collections::Cloned;

assert_eq!([&13].cloned_(), [13]);
assert_eq!([&13, &21].cloned_(), [13, 21]);
assert_eq!([&13, &21, &34].cloned_(), [13, 21, 34]);
assert_eq!([&13, &21, &34, &55].cloned_(), [13, 21, 34, 55]);

“alloc” feature

This demonstrates how &str and &[T] elements can be cloned with the “alloc” feature

use core_extensions::collections::Cloned;

assert_eq!(["foo"].cloned_(), ["foo".to_string()]);
assert_eq!(["bar", "baz"].cloned_(), ["bar".to_string(), "baz".to_string()]);

assert_eq!((&[3, 5, 8][..],).cloned_(), (vec![3, 5, 8],));
assert_eq!((&[13, 21][..], &[34, 55][..]).cloned_(), (vec![13, 21], vec![34, 55]));

Implementing this trait

use core_extensions::collections::Cloned;

#[derive(Debug, PartialEq)]
struct Pair<T>(T, T);

impl<T: Cloned> Cloned for Pair<T> {
    type Cloned = Pair<T::Cloned>;

    fn cloned_(&self) -> Self::Cloned {
        Pair(self.0.cloned_(), self.1.cloned_())
    }
}

let foo = Pair(&100, &200);
assert_eq!(foo.cloned_(), Pair(100, 200));

let bar = Pair(Some(&81), None);
assert_eq!(bar.cloned_(), Pair(Some(81), None));

let baz = Pair([&3, &5], [&8, &13]);
assert_eq!(baz.cloned_(), Pair([3, 5], [8, 13]));

Required Associated Types

The type of this with owned values instead of references to them.

Required Methods

Clones a collection of references into a collection of values.

Implementations on Foreign Types

When the “const_params” feature is disabled, the Cloned trait is implemented for arrays up to 32 elements long.

Implementors