Function core_extensions::collections::clone_this
source · [−]pub fn clone_this<T>(this: &T) -> T::Owned where
T: ?Sized + CloneBound,
Available on crate feature
collections
only.Expand description
For cloning something with either Clone
or ToOwned
depending on
the “alloc” feature.
Features
If the "alloc"
feature is disabled then this requires Clone
,
if it’s enabled this requires ToOwned
.
ToOwned
is implemented for all types that implement Clone
,
and you can’t call this function on &str
or &[T]
arguments
without enabling the “alloc” feature.
Examples
use core_extensions::collections::clone_this;
assert_eq!(clone_this(&0), 0);
assert_eq!(clone_this(&"hello"), "hello");
With the “alloc” feature enabled you can clone &str
into String
,
and &[T]
into Vec<T>
.
use core_extensions::collections::clone_this;
assert_eq!(clone_this("world"), "world".to_string());
assert_eq!(clone_this(&[3, 5, 8][..]), vec![3, 5, 8]);