pub trait Clone {
fn clone(&self) -> Self;
fn clone_from(&mut self, source: &Self) { ... }
}
Expand description
A common trait for the ability to explicitly duplicate an object.
Differs from Copy
in that Copy
is implicit and an inexpensive bit-wise copy, while
Clone
is always explicit and may or may not be expensive. In order to enforce
these characteristics, Rust does not allow you to reimplement Copy
, but you
may reimplement Clone
and run arbitrary code.
Since Clone
is more general than Copy
, you can automatically make anything
Copy
be Clone
as well.
Derivable
This trait can be used with #[derive]
if all fields are Clone
. The derive
d
implementation of Clone
calls clone
on each field.
For a generic struct, #[derive]
implements Clone
conditionally by adding bound Clone
on
generic parameters.
// `derive` implements Clone for Reading<T> when T is Clone.
#[derive(Clone)]
struct Reading<T> {
frequency: T,
}
How can I implement Clone
?
Types that are Copy
should have a trivial implementation of Clone
. More formally:
if T: Copy
, x: T
, and y: &T
, then let x = y.clone();
is equivalent to let x = *y;
.
Manual implementations should be careful to uphold this invariant; however, unsafe code
must not rely on it to ensure memory safety.
An example is a generic struct holding a function pointer. In this case, the
implementation of Clone
cannot be derive
d, but can be implemented as:
struct Generate<T>(fn() -> T);
impl<T> Copy for Generate<T> {}
impl<T> Clone for Generate<T> {
fn clone(&self) -> Self {
*self
}
}
Additional implementors
In addition to the implementors listed below,
the following types also implement Clone
:
- Function item types (i.e., the distinct types defined for each function)
- Function pointer types (e.g.,
fn() -> i32
) - Closure types, if they capture no value from the environment
or if all such captured values implement
Clone
themselves. Note that variables captured by shared reference always implementClone
(even if the referent doesn’t), while variables captured by mutable reference never implementClone
.
Required Methods
Provided Methods
fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
.
a.clone_from(&b)
is equivalent to a = b.clone()
in functionality,
but can be overridden to reuse the resources of a
to avoid unnecessary
allocations.
Implementors
impl Clone for EntityIndex
impl Clone for wasmtime_environ::EntityType
impl Clone for GlobalInit
impl Clone for MemoryStyle
impl Clone for wasmtime_environ::ModuleType
impl Clone for SettingKind
impl Clone for TableStyle
impl Clone for TrapCode
impl Clone for WasmType
impl Clone for AliasKind
impl Clone for BlockType
impl Clone for CanonicalOption
impl Clone for ComponentArgKind
impl Clone for ComponentFunction
impl Clone for CustomSectionKind
impl Clone for wasmtime_environ::wasmparser::Encoding
impl Clone for ExternalKind
impl Clone for wasmtime_environ::wasmparser::InterfaceTypeRef
impl Clone for LinkingType
impl Clone for ModuleArgKind
impl Clone for NameType
impl Clone for PrimitiveInterfaceType
impl Clone for RelocType
impl Clone for TagKind
impl Clone for Type
impl Clone for TypeDef
impl Clone for TypeRef
impl Clone for ComponentEntityType
impl Clone for wasmtime_environ::wasmparser::types::EntityType
impl Clone for InstanceTypeKind
impl Clone for wasmtime_environ::wasmparser::types::InterfaceType
impl Clone for wasmtime_environ::wasmparser::types::InterfaceTypeRef
impl Clone for ModuleInstanceTypeKind
impl Clone for wasmtime_environ::__core::cmp::Ordering
impl Clone for Infallible
impl Clone for Alignment
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for Which
impl Clone for SearchStep
impl Clone for wasmtime_environ::__core::sync::atomic::Ordering
impl Clone for TryReserveErrorKind
impl Clone for VarError
impl Clone for SeekFrom
impl Clone for ErrorKind
impl Clone for SocketAddr
impl Clone for Shutdown
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for BacktraceStyle
impl Clone for RecvTimeoutError
impl Clone for TryRecvError
impl Clone for DwarfFileType
impl Clone for Format
impl Clone for gimli::common::SectionId
impl Clone for RunTimeEndian
impl Clone for Pointer
impl Clone for gimli::read::Error
impl Clone for ColumnType
impl Clone for Value
impl Clone for ValueType
impl Clone for Level
impl Clone for LevelFilter
impl Clone for AddressSize
impl Clone for object::common::Architecture
impl Clone for object::common::BinaryFormat
impl Clone for ComdatKind
impl Clone for FileFlags
impl Clone for RelocationEncoding
impl Clone for RelocationKind
impl Clone for SectionFlags
impl Clone for SectionKind
impl Clone for SegmentFlags
impl Clone for SymbolKind
impl Clone for SymbolScope
impl Clone for object::endian::Endianness
impl Clone for CompressionFormat
impl Clone for FileKind
impl Clone for ObjectKind
impl Clone for RelocationTarget
impl Clone for object::read::SymbolSection
impl Clone for Mangling
impl Clone for StandardSection
impl Clone for StandardSegment
impl Clone for object::write::SymbolSection
impl Clone for CDataModel
impl Clone for Size
impl Clone for ParseError
impl Clone for Aarch64Architecture
impl Clone for target_lexicon::targets::Architecture
impl Clone for ArmArchitecture
impl Clone for target_lexicon::targets::BinaryFormat
impl Clone for CustomVendor
impl Clone for Environment
impl Clone for Mips32Architecture
impl Clone for Mips64Architecture
impl Clone for OperatingSystem
impl Clone for Riscv32Architecture
impl Clone for Riscv64Architecture
impl Clone for Vendor
impl Clone for X86_32Architecture
impl Clone for CallingConvention
impl Clone for target_lexicon::triple::Endianness
impl Clone for PointerWidth
impl Clone for bool
impl Clone for char
impl Clone for f32
impl Clone for f64
impl Clone for i8
impl Clone for i16
impl Clone for i32
impl Clone for i64
impl Clone for i128
impl Clone for isize
impl Clone for !
impl Clone for u8
impl Clone for u16
impl Clone for u32
impl Clone for u64
impl Clone for u128
impl Clone for usize
impl Clone for AnyfuncIndex
impl Clone for BuiltinFunctionIndex
impl Clone for DataIndex
impl Clone for DefinedFuncIndex
impl Clone for DefinedGlobalIndex
impl Clone for DefinedMemoryIndex
impl Clone for DefinedTableIndex
impl Clone for ElemIndex
impl Clone for FilePos
impl Clone for FuncIndex
impl Clone for wasmtime_environ::Global
impl Clone for GlobalIndex
impl Clone for InstructionAddressMap
impl Clone for Memory
impl Clone for MemoryIndex
impl Clone for MemoryInitializer
impl Clone for MemoryPlan
impl Clone for Setting
impl Clone for SignatureIndex
impl Clone for StaticMemoryInitializer
impl Clone for Table
impl Clone for TableIndex
impl Clone for TableInitializer
impl Clone for TablePlan
impl Clone for Tag
impl Clone for TagIndex
impl Clone for TrapInformation
impl Clone for Tunables
impl Clone for TypeIndex
impl Clone for WasmFuncType
impl Clone for BinaryReaderError
impl Clone for ComponentStartFunction
impl Clone for FuncType
impl Clone for GlobalType
impl Clone for Ieee32
impl Clone for Ieee64
impl Clone for MemoryImmediate
impl Clone for MemoryType
impl Clone for Parser
impl Clone for Reloc
impl Clone for TableType
impl Clone for TagType
impl Clone for V128
impl Clone for WasmFeatures
impl Clone for wasmtime_environ::wasmparser::types::ComponentFuncType
impl Clone for wasmtime_environ::wasmparser::types::ComponentType
impl Clone for wasmtime_environ::wasmparser::types::InstanceType
impl Clone for ModuleInstanceType
impl Clone for wasmtime_environ::wasmparser::types::ModuleType
impl Clone for RecordType
impl Clone for TupleType
impl Clone for wasmtime_environ::wasmparser::types::TypeId
impl Clone for UnionType
impl Clone for wasmtime_environ::wasmparser::types::VariantCase
impl Clone for VariantType
impl Clone for AllocError
impl Clone for Layout
impl Clone for LayoutError
impl Clone for wasmtime_environ::__core::any::TypeId
impl Clone for CpuidResult
impl Clone for __m128
impl Clone for __m128bh
impl Clone for __m128d
impl Clone for __m128i
impl Clone for __m256
impl Clone for __m256bh
impl Clone for __m256d
impl Clone for __m256i
impl Clone for __m512
impl Clone for __m512bh
impl Clone for __m512d
impl Clone for __m512i
impl Clone for TryFromSliceError
impl Clone for wasmtime_environ::__core::ascii::EscapeDefault
impl Clone for CharTryFromError
impl Clone for DecodeUtf16Error
impl Clone for wasmtime_environ::__core::char::EscapeDebug
impl Clone for wasmtime_environ::__core::char::EscapeDefault
impl Clone for wasmtime_environ::__core::char::EscapeUnicode
impl Clone for ParseCharError
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for TryFromCharError
impl Clone for FromBytesUntilNulError
impl Clone for FromBytesWithNulError
impl Clone for wasmtime_environ::__core::fmt::Error
impl Clone for SipHasher
impl Clone for PhantomPinned
impl Clone for NonZeroI8
impl Clone for NonZeroI16
impl Clone for NonZeroI32
impl Clone for NonZeroI64
impl Clone for NonZeroI128
impl Clone for NonZeroIsize
impl Clone for NonZeroU8
impl Clone for NonZeroU16
impl Clone for NonZeroU32
impl Clone for NonZeroU64
impl Clone for NonZeroU128
impl Clone for NonZeroUsize
impl Clone for ParseFloatError
impl Clone for ParseIntError
impl Clone for TryFromIntError
impl Clone for RangeFull
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for Duration
impl Clone for FromFloatSecsError
impl Clone for alloc::alloc::Global
impl Clone for Box<str, Global>
impl Clone for Box<CStr, Global>
impl Clone for Box<OsStr, Global>
impl Clone for Box<Path, Global>
impl Clone for alloc::collections::TryReserveError
impl Clone for CString
impl Clone for FromVecWithNulError
impl Clone for IntoStringError
impl Clone for NulError
impl Clone for FromUtf8Error
impl Clone for String
impl Clone for System
impl Clone for DefaultHasher
impl Clone for std::collections::hash::map::RandomState
impl Clone for OsString
impl Clone for FileType
impl Clone for std::fs::Metadata
impl Clone for OpenOptions
impl Clone for Permissions
impl Clone for std::io::util::Empty
impl Clone for Sink
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for AddrParseError
impl Clone for InvalidHandleError
impl Clone for NullHandleError
impl Clone for PathBuf
impl Clone for StripPrefixError
impl Clone for ExitCode
impl Clone for ExitStatus
impl Clone for ExitStatusError
impl Clone for Output
impl Clone for WaitTimeoutResult
impl Clone for RecvError
impl Clone for AccessError
impl Clone for Thread
impl Clone for ThreadId
impl Clone for Instant
impl Clone for SystemTime
impl Clone for SystemTimeError
impl Clone for getrandom::error::Error
impl Clone for AArch64
impl Clone for Arm
impl Clone for RiscV
impl Clone for X86
impl Clone for X86_64
impl Clone for DebugTypeSignature
impl Clone for DwoId
impl Clone for gimli::common::Encoding
impl Clone for LineEncoding
impl Clone for Register
impl Clone for DwAccess
impl Clone for DwAddr
impl Clone for DwAt
impl Clone for DwAte
impl Clone for DwCc
impl Clone for DwCfa
impl Clone for DwChildren
impl Clone for DwDefaulted
impl Clone for DwDs
impl Clone for DwDsc
impl Clone for DwEhPe
impl Clone for DwEnd
impl Clone for DwForm
impl Clone for DwId
impl Clone for DwIdx
impl Clone for DwInl
impl Clone for DwLang
impl Clone for DwLle
impl Clone for DwLnct
impl Clone for DwLne
impl Clone for DwLns
impl Clone for DwMacro
impl Clone for DwOp
impl Clone for DwOrd
impl Clone for DwRle
impl Clone for DwSect
impl Clone for DwSectV2
impl Clone for DwTag
impl Clone for DwUt
impl Clone for DwVirtuality
impl Clone for DwVis
impl Clone for gimli::endianity::BigEndian
impl Clone for gimli::endianity::LittleEndian
impl Clone for Abbreviation
impl Clone for Abbreviations
impl Clone for AttributeSpecification
impl Clone for ArangeEntry
impl Clone for Augmentation
impl Clone for BaseAddresses
impl Clone for SectionBaseAddresses
impl Clone for UnitIndexSection
impl Clone for FileEntryFormat
impl Clone for LineRow
impl Clone for ReaderOffsetId
impl Clone for gimli::read::rnglists::Range
impl Clone for StoreOnHeap
impl Clone for Ident
impl Clone for object::endian::BigEndian
impl Clone for object::endian::LittleEndian
impl Clone for VersionIndex
impl Clone for CompressedFileRange
impl Clone for object::read::Error
impl Clone for object::read::SectionIndex
impl Clone for object::read::SymbolIndex
impl Clone for FileHeader
impl Clone for ProgramHeader
impl Clone for Rel
impl Clone for SectionHeader
impl Clone for object::write::elf::writer::SectionIndex
impl Clone for Sym
impl Clone for object::write::elf::writer::SymbolIndex
impl Clone for object::write::elf::writer::Verdef
impl Clone for object::write::elf::writer::Vernaux
impl Clone for object::write::elf::writer::Verneed
impl Clone for StringId
impl Clone for ComdatId
impl Clone for object::write::Error
impl Clone for object::write::SectionId
impl Clone for SymbolId
impl Clone for IgnoredAny
impl Clone for serde::de::value::Error
impl Clone for Triple
impl Clone for AHasher
impl Clone for FinderBuilder
impl Clone for Prefilter
impl Clone for RandomState
impl Clone for TryReserveError
impl Clone for TryReserveError
impl<'_, A> Clone for wasmtime_environ::__core::option::Iter<'_, A>
impl<'_, B> Clone for Cow<'_, B> where
B: ToOwned + ?Sized,
impl<'_, K> Clone for std::collections::hash::set::Iter<'_, K>
impl<'_, K> Clone for Iter<'_, K>
impl<'_, K> Clone for Iter<'_, K>
impl<'_, K, V> Clone for alloc::collections::btree::map::Iter<'_, K, V>
impl<'_, K, V> Clone for alloc::collections::btree::map::Keys<'_, K, V>
impl<'_, K, V> Clone for alloc::collections::btree::map::Range<'_, K, V>
impl<'_, K, V> Clone for alloc::collections::btree::map::Values<'_, K, V>
impl<'_, K, V> Clone for std::collections::hash::map::Iter<'_, K, V>
impl<'_, K, V> Clone for std::collections::hash::map::Keys<'_, K, V>
impl<'_, K, V> Clone for std::collections::hash::map::Values<'_, K, V>
impl<'_, K, V> Clone for indexmap::map::Iter<'_, K, V>
impl<'_, K, V> Clone for indexmap::map::Keys<'_, K, V>
impl<'_, K, V> Clone for indexmap::map::Values<'_, K, V>
impl<'_, K, V> Clone for Iter<'_, K, V>
impl<'_, K, V> Clone for Iter<'_, K, V>
impl<'_, K, V> Clone for Keys<'_, K, V>
impl<'_, K, V> Clone for Keys<'_, K, V>
impl<'_, K, V> Clone for Values<'_, K, V>
impl<'_, K, V> Clone for Values<'_, K, V>
impl<'_, T> !Clone for &mut T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
impl<'_, T> Clone for &T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!