Trait rustc_std_workspace_std::prelude::v1::Clone
1.0.0 · source · pub trait Clone: Sized {
// Required method
fn clone(&self) -> Self;
// Provided method
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§
sourcefn 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 hashbrown::TryReserveError
impl Clone for DIR
impl Clone for FILE
impl Clone for fpos_t
impl Clone for timezone
impl Clone for fpos64_t
impl Clone for AsciiChar
impl Clone for rustc_std_workspace_std::cmp::Ordering
impl Clone for TryReserveErrorKind
impl Clone for Infallible
impl Clone for VarError
impl Clone for rustc_std_workspace_std::fmt::Alignment
impl Clone for ErrorKind
impl Clone for SeekFrom
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for Shutdown
impl Clone for rustc_std_workspace_std::net::SocketAddr
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for BacktraceStyle
impl Clone for Which
impl Clone for SearchStep
impl Clone for rustc_std_workspace_std::sync::atomic::Ordering
impl Clone for RecvTimeoutError
impl Clone for TryRecvError
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 FromBytesUntilNulError
impl Clone for TimSortRun
impl Clone for in6_addr
impl Clone for termios2
impl Clone for sem_t
impl Clone for msqid_ds
impl Clone for semid_ds
impl Clone for sigset_t
impl Clone for sysinfo
impl Clone for clone_args
impl Clone for max_align_t
impl Clone for statvfs
impl Clone for _libc_fpstate
impl Clone for _libc_fpxreg
impl Clone for _libc_xmmreg
impl Clone for flock64
impl Clone for flock
impl Clone for ipc_perm
impl Clone for mcontext_t
impl Clone for pthread_attr_t
impl Clone for ptrace_rseq_configuration
impl Clone for seccomp_notif_sizes
impl Clone for shmid_ds
impl Clone for sigaction
impl Clone for siginfo_t
impl Clone for stack_t
impl Clone for stat64
impl Clone for libc::unix::linux_like::linux::gnu::b64::x86_64::stat
impl Clone for statfs64
impl Clone for statfs
impl Clone for statvfs64
impl Clone for ucontext_t
impl Clone for user
impl Clone for user_fpregs_struct
impl Clone for user_regs_struct
impl Clone for Elf32_Chdr
impl Clone for Elf64_Chdr
impl Clone for __c_anonymous_ptrace_syscall_info_entry
impl Clone for __c_anonymous_ptrace_syscall_info_exit
impl Clone for __c_anonymous_ptrace_syscall_info_seccomp
impl Clone for __exit_status
impl Clone for __timeval
impl Clone for aiocb
impl Clone for cmsghdr
impl Clone for glob64_t
impl Clone for mallinfo2
impl Clone for mallinfo
impl Clone for msghdr
impl Clone for nl_mmap_hdr
impl Clone for nl_mmap_req
impl Clone for nl_pktinfo
impl Clone for ntptimeval
impl Clone for ptrace_peeksiginfo_args
impl Clone for ptrace_syscall_info
impl Clone for regex_t
impl Clone for rtentry
impl Clone for seminfo
impl Clone for statx
impl Clone for statx_timestamp
impl Clone for termios
impl Clone for timex
impl Clone for utmpx
impl Clone for open_how
impl Clone for Elf32_Ehdr
impl Clone for Elf32_Phdr
impl Clone for Elf32_Shdr
impl Clone for Elf32_Sym
impl Clone for Elf64_Ehdr
impl Clone for Elf64_Phdr
impl Clone for Elf64_Shdr
impl Clone for Elf64_Sym
impl Clone for __c_anonymous_ifru_map
impl Clone for __c_anonymous_sockaddr_can_j1939
impl Clone for __c_anonymous_sockaddr_can_tp
impl Clone for af_alg_iv
impl Clone for arpd_request
impl Clone for can_filter
impl Clone for can_frame
impl Clone for canfd_frame
impl Clone for canxl_frame
impl Clone for cpu_set_t
impl Clone for dirent64
impl Clone for dirent
impl Clone for dl_phdr_info
impl Clone for dqblk
impl Clone for fanotify_event_metadata
impl Clone for fanotify_response
impl Clone for ff_condition_effect
impl Clone for ff_constant_effect
impl Clone for ff_effect
impl Clone for ff_envelope
impl Clone for ff_periodic_effect
impl Clone for ff_ramp_effect
impl Clone for ff_replay
impl Clone for ff_rumble_effect
impl Clone for ff_trigger
impl Clone for file_clone_range
impl Clone for fsid_t
impl Clone for genlmsghdr
impl Clone for glob_t
impl Clone for hwtstamp_config
impl Clone for if_nameindex
impl Clone for ifreq
impl Clone for in6_ifreq
impl Clone for in6_pktinfo
impl Clone for inotify_event
impl Clone for input_absinfo
impl Clone for input_event
impl Clone for input_id
impl Clone for input_keymap_entry
impl Clone for input_mask
impl Clone for itimerspec
impl Clone for j1939_filter
impl Clone for mntent
impl Clone for mq_attr
impl Clone for msginfo
impl Clone for nlattr
impl Clone for nlmsgerr
impl Clone for nlmsghdr
impl Clone for packet_mreq
impl Clone for passwd
impl Clone for posix_spawn_file_actions_t
impl Clone for posix_spawnattr_t
impl Clone for pthread_barrier_t
impl Clone for pthread_barrierattr_t
impl Clone for pthread_cond_t
impl Clone for pthread_condattr_t
impl Clone for pthread_mutex_t
impl Clone for pthread_mutexattr_t
impl Clone for pthread_rwlock_t
impl Clone for pthread_rwlockattr_t
impl Clone for regmatch_t
impl Clone for rlimit64
impl Clone for sctp_authinfo
impl Clone for sctp_initmsg
impl Clone for sctp_nxtinfo
impl Clone for sctp_prinfo
impl Clone for sctp_rcvinfo
impl Clone for sctp_sndinfo
impl Clone for sctp_sndrcvinfo
impl Clone for seccomp_data
impl Clone for sembuf
impl Clone for signalfd_siginfo
impl Clone for sock_extended_err
impl Clone for sock_filter
impl Clone for sock_fprog
impl Clone for sock_txtime
impl Clone for sockaddr_alg
impl Clone for sockaddr_can
impl Clone for sockaddr_nl
impl Clone for sockaddr_vm
impl Clone for spwd
impl Clone for ucred
impl Clone for uinput_abs_setup
impl Clone for uinput_ff_erase
impl Clone for uinput_ff_upload
impl Clone for uinput_setup
impl Clone for uinput_user_dev
impl Clone for Dl_info
impl Clone for addrinfo
impl Clone for arphdr
impl Clone for arpreq
impl Clone for arpreq_old
impl Clone for epoll_event
impl Clone for fd_set
impl Clone for ifaddrs
impl Clone for in6_rtmsg
impl Clone for in_addr
impl Clone for in_pktinfo
impl Clone for ip_mreq
impl Clone for ip_mreq_source
impl Clone for ip_mreqn
impl Clone for lconv
impl Clone for mmsghdr
impl Clone for sched_param
impl Clone for sigevent
impl Clone for sockaddr
impl Clone for sockaddr_in6
impl Clone for sockaddr_in
impl Clone for sockaddr_ll
impl Clone for sockaddr_storage
impl Clone for sockaddr_un
impl Clone for tm
impl Clone for utsname
impl Clone for group
impl Clone for hostent
impl Clone for iovec
impl Clone for ipv6_mreq
impl Clone for itimerval
impl Clone for linger
impl Clone for pollfd
impl Clone for protoent
impl Clone for rlimit
impl Clone for rusage
impl Clone for servent
impl Clone for sigval
impl Clone for timespec
impl Clone for timeval
impl Clone for tms
impl Clone for utimbuf
impl Clone for winsize
impl Clone for TryDemangleError
impl Clone for AllocError
impl Clone for Global
impl Clone for Layout
impl Clone for LayoutError
impl Clone for System
impl Clone for 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 rustc_std_workspace_std::ascii::EscapeDefault
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 CharTryFromError
impl Clone for DecodeUtf16Error
impl Clone for rustc_std_workspace_std::char::EscapeDebug
impl Clone for rustc_std_workspace_std::char::EscapeDefault
impl Clone for rustc_std_workspace_std::char::EscapeUnicode
impl Clone for ParseCharError
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for TryFromCharError
impl Clone for DefaultHasher
impl Clone for RandomState
impl Clone for rustc_std_workspace_std::collections::TryReserveError
impl Clone for CString
impl Clone for FromBytesWithNulError
impl Clone for FromVecWithNulError
impl Clone for IntoStringError
impl Clone for NulError
impl Clone for OsString
impl Clone for Error
impl Clone for FileTimes
impl Clone for FileType
impl Clone for Metadata
impl Clone for OpenOptions
impl Clone for Permissions
impl Clone for SipHasher
impl Clone for rustc_std_workspace_std::io::Empty
impl Clone for Sink
impl Clone for PhantomPinned
impl Clone for Assume
impl Clone for AddrParseError
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
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 rustc_std_workspace_std::os::linux::raw::stat
impl Clone for rustc_std_workspace_std::os::unix::net::SocketAddr
impl Clone for SocketCred
impl Clone for UCred
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 rustc_std_workspace_std::ptr::Alignment
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for FromUtf8Error
impl Clone for String
impl Clone for RecvError
impl Clone for WaitTimeoutResult
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for AccessError
impl Clone for Thread
impl Clone for ThreadId
impl Clone for Duration
impl Clone for Instant
impl Clone for SystemTime
impl Clone for SystemTimeError
impl Clone for TryFromFloatSecsError
impl Clone for __c_anonymous_ptrace_syscall_info_data
impl Clone for __c_anonymous_ifr_ifru
impl Clone for __c_anonymous_sockaddr_can_can_addr
impl Clone for _Unwind_Action
impl Clone for _Unwind_Reason_Code
impl<'a> Clone for Component<'a>
impl<'a> Clone for Prefix<'a>
impl<'a> Clone for Source<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Clone for IoSlice<'a>
impl<'a> Clone for Location<'a>
impl<'a> Clone for Ancestors<'a>
impl<'a> Clone for Components<'a>
impl<'a> Clone for rustc_std_workspace_std::path::Iter<'a>
impl<'a> Clone for PrefixComponent<'a>
impl<'a> Clone for EscapeAscii<'a>
impl<'a> Clone for CharSearcher<'a>
impl<'a> Clone for Bytes<'a>
impl<'a> Clone for CharIndices<'a>
impl<'a> Clone for Chars<'a>
impl<'a> Clone for EncodeUtf16<'a>
impl<'a> Clone for rustc_std_workspace_std::str::EscapeDebug<'a>
impl<'a> Clone for rustc_std_workspace_std::str::EscapeDefault<'a>
impl<'a> Clone for rustc_std_workspace_std::str::EscapeUnicode<'a>
impl<'a> Clone for Lines<'a>
impl<'a> Clone for LinesAny<'a>
impl<'a> Clone for SplitAsciiWhitespace<'a>
impl<'a> Clone for SplitWhitespace<'a>
impl<'a> Clone for Utf8Chunk<'a>
impl<'a> Clone for Utf8Chunks<'a>
impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>
impl<'a, 'b> Clone for StrSearcher<'a, 'b>
impl<'a, 'b, const N: usize> Clone for CharArrayRefSearcher<'a, 'b, N>
impl<'a, F> Clone for CharPredicateSearcher<'a, F>where F: Clone + FnMut(char) -> bool,
impl<'a, P> Clone for MatchIndices<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for Matches<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatchIndices<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatches<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for rustc_std_workspace_std::str::RSplit<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RSplitN<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RSplitTerminator<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for rustc_std_workspace_std::str::Split<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for rustc_std_workspace_std::str::SplitInclusive<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for SplitN<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for SplitTerminator<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, T> Clone for RChunksExact<'a, T>
impl<'a, T, const N: usize> Clone for ArrayWindows<'a, T, N>where T: Clone + 'a,
impl<'a, const N: usize> Clone for CharArraySearcher<'a, N>
impl<'f> Clone for VaListImpl<'f>
impl<'fd> Clone for BorrowedFd<'fd>
impl<A> Clone for Repeat<A>where A: Clone,
impl<A> Clone for rustc_std_workspace_std::option::IntoIter<A>where A: Clone,
impl<A> Clone for rustc_std_workspace_std::option::Iter<'_, A>
impl<A, B> Clone for Chain<A, B>where A: Clone, B: Clone,
impl<A, B> Clone for Zip<A, B>where A: Clone, B: Clone,
impl<B> Clone for Cow<'_, B>where B: ToOwned + ?Sized,
impl<B, C> Clone for ControlFlow<B, C>where B: Clone, C: Clone,
impl<Dyn> Clone for DynMetadata<Dyn>where Dyn: ?Sized,
impl<F> Clone for FromFn<F>where F: Clone,
impl<F> Clone for OnceWith<F>where F: Clone,
impl<F> Clone for RepeatWith<F>where F: Clone,
impl<H> Clone for BuildHasherDefault<H>
impl<I> Clone for FromIter<I>where I: Clone,
impl<I> Clone for DecodeUtf16<I>where I: Clone + Iterator<Item = u16>,
impl<I> Clone for Cloned<I>where I: Clone,
impl<I> Clone for Copied<I>where I: Clone,
impl<I> Clone for Cycle<I>where I: Clone,
impl<I> Clone for Enumerate<I>where I: Clone,
impl<I> Clone for Fuse<I>where I: Clone,
impl<I> Clone for Intersperse<I>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<I> Clone for Peekable<I>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<I> Clone for Skip<I>where I: Clone,
impl<I> Clone for StepBy<I>where I: Clone,
impl<I> Clone for Take<I>where I: Clone,
impl<I, F> Clone for FilterMap<I, F>where I: Clone, F: Clone,
impl<I, F> Clone for Inspect<I, F>where I: Clone, F: Clone,
impl<I, F> Clone for Map<I, F>where I: Clone, F: Clone,
impl<I, G> Clone for IntersperseWith<I, G>where I: Iterator + Clone, <I as Iterator>::Item: Clone, G: Clone,
impl<I, P> Clone for Filter<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for MapWhile<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for SkipWhile<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for TakeWhile<I, P>where I: Clone, P: Clone,
impl<I, St, F> Clone for Scan<I, St, F>where I: Clone, St: Clone, F: Clone,
impl<I, U> Clone for Flatten<I>where I: Clone + Iterator, <I as Iterator>::Item: IntoIterator<IntoIter = U, Item = <U as Iterator>::Item>, U: Clone + Iterator,
impl<I, U, F> Clone for FlatMap<I, U, F>where I: Clone, F: Clone, U: Clone + IntoIterator, <U as IntoIterator>::IntoIter: Clone,
impl<I, const N: usize> Clone for rustc_std_workspace_std::iter::ArrayChunks<I, N>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<Idx> Clone for rustc_std_workspace_std::ops::Range<Idx>where Idx: Clone,
impl<Idx> Clone for RangeFrom<Idx>where Idx: Clone,
impl<Idx> Clone for RangeInclusive<Idx>where Idx: Clone,
impl<Idx> Clone for RangeTo<Idx>where Idx: Clone,
impl<Idx> Clone for RangeToInclusive<Idx>where Idx: Clone,
impl<K> Clone for hashbrown::set::Iter<'_, K>
impl<K> Clone for rustc_std_workspace_std::collections::hash_set::Iter<'_, K>
impl<K, V> Clone for hashbrown::map::Iter<'_, K, V>
impl<K, V> Clone for hashbrown::map::Keys<'_, K, V>
impl<K, V> Clone for hashbrown::map::Values<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::btree_map::Cursor<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::btree_map::Iter<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::btree_map::Keys<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::btree_map::Range<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::btree_map::Values<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::hash_map::Iter<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::hash_map::Keys<'_, K, V>
impl<K, V> Clone for rustc_std_workspace_std::collections::hash_map::Values<'_, K, V>
impl<K, V, A> Clone for BTreeMap<K, V, A>where K: Clone, V: Clone, A: Allocator + Clone,
impl<K, V, S> Clone for rustc_std_workspace_std::collections::HashMap<K, V, S>where K: Clone, V: Clone, S: Clone,
impl<K, V, S, A> Clone for hashbrown::map::HashMap<K, V, S, A>where K: Clone, V: Clone, S: Clone, A: Allocator + Clone,
impl<P> Clone for Pin<P>where P: Clone,
impl<T> !Clone for &mut Twhere T: ?Sized,
Shared references can be cloned, but mutable references cannot!
impl<T> Clone for Bound<T>where T: Clone,
impl<T> Clone for Option<T>where T: Clone,
impl<T> Clone for TrySendError<T>where T: Clone,
impl<T> Clone for Poll<T>where T: Clone,
impl<T> Clone for *const Twhere T: ?Sized,
impl<T> Clone for *mut Twhere T: ?Sized,
impl<T> Clone for &Twhere T: ?Sized,
Shared references can be cloned, but mutable references cannot!