Movatterモバイル変換


[0]ホーム

URL:


Copy

std::marker

TraitCopy 

1.0.0 ·Source
pub trait Copy:Clone { }
Expand description

Types whose values can be duplicated simply by copying bits.

By default, variable bindings have ‘move semantics.’ In otherwords:

#[derive(Debug)]structFoo;letx = Foo;lety = x;// `x` has moved into `y`, and so cannot be used// println!("{x:?}"); // error: use of moved value

However, if a type implementsCopy, it instead has ‘copy semantics’:

// We can derive a `Copy` implementation. `Clone` is also required, as it's// a supertrait of `Copy`.#[derive(Debug, Copy, Clone)]structFoo;letx = Foo;lety = x;// `y` is a copy of `x`println!("{x:?}");// A-OK!

It’s important to note that in these two examples, the only difference is whether youare allowed to accessx after the assignment. Under the hood, both a copy and a movecan result in bits being copied in memory, although this is sometimes optimized away.

§How can I implementCopy?

There are two ways to implementCopy on your type. The simplest is to usederive:

#[derive(Copy, Clone)]structMyStruct;

You can also implementCopy andClone manually:

structMyStruct;implCopyforMyStruct { }implCloneforMyStruct {fnclone(&self) -> MyStruct {*self}}

There is a small difference between the two. Thederive strategy will also place aCopybound on type parameters:

#[derive(Clone)]structMyStruct<T>(T);impl<T: Copy> CopyforMyStruct<T> { }

This isn’t always desired. For example, shared references (&T) can be copied regardless ofwhetherT isCopy. Likewise, a generic struct containing markers such asPhantomDatacould potentially be duplicated with a bit-wise copy.

§What’s the difference betweenCopy andClone?

Copies happen implicitly, for example as part of an assignmenty = x. The behavior ofCopy is not overloadable; it is always a simple bit-wise copy.

Cloning is an explicit action,x.clone(). The implementation ofClone canprovide any type-specific behavior necessary to duplicate values safely. For example,the implementation ofClone forString needs to copy the pointed-to stringbuffer in the heap. A simple bitwise copy ofString values would merely copy thepointer, leading to a double free down the line. For this reason,String isClonebut notCopy.

Clone is a supertrait ofCopy, so everything which isCopy must also implementClone. If a type isCopy then itsClone implementation only needs to return*self(see the example above).

§When can my type beCopy?

A type can implementCopy if all of its components implementCopy. For example, thisstruct can beCopy:

#[derive(Copy, Clone)]structPoint {   x: i32,   y: i32,}

A struct can beCopy, andi32 isCopy, thereforePoint is eligible to beCopy.By contrast, consider

structPointList {    points: Vec<Point>,}

The structPointList cannot implementCopy, becauseVec<T> is notCopy. If weattempt to derive aCopy implementation, we’ll get an error:

the trait `Copy` cannot be implemented for this type; field `points` does not implement `Copy`

Shared references (&T) are alsoCopy, so a type can beCopy, even when it holdsshared references of typesT that arenotCopy. Consider the following struct,which can implementCopy, because it only holds ashared reference to our non-CopytypePointList from above:

#[derive(Copy, Clone)]structPointListWrapper<'a> {    point_list_ref:&'aPointList,}

§Whencan’t my type beCopy?

Some types can’t be copied safely. For example, copying&mut T would create an aliasedmutable reference. CopyingString would duplicate responsibility for managing theString’s buffer, leading to a double free.

Generalizing the latter case, any type implementingDrop can’t beCopy, because it’smanaging some resource besides its ownsize_of::<T> bytes.

If you try to implementCopy on a struct or enum containing non-Copy data, you will getthe errorE0204.

§Whenshould my type beCopy?

Generally speaking, if your typecan implementCopy, it should. Keep in mind, though,that implementingCopy is part of the public API of your type. If the type might becomenon-Copy in the future, it could be prudent to omit theCopy implementation now, toavoid a breaking API change.

§Additional implementors

In addition to theimplementors listed below,the following types also implementCopy:

  • 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 environmentor if all such captured values implementCopy themselves.Note that variables captured by shared reference always implementCopy(even if the referent doesn’t),while variables captured by mutable reference never implementCopy.

Dyn Compatibility§

This trait isnotdyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

implCopy forAsciiChar

1.0.0 ·Source§

implCopy for std::cmp::Ordering

1.34.0 ·Source§

implCopy forInfallible

1.64.0 ·Source§

implCopy forFromBytesWithNulError

1.28.0 ·Source§

implCopy for std::fmt::Alignment

Source§

implCopy forDebugAsHex

Source§

implCopy forSign

1.0.0 ·Source§

implCopy forErrorKind

1.0.0 ·Source§

implCopy forSeekFrom

1.7.0 ·Source§

implCopy forIpAddr

Source§

implCopy forIpv6MulticastScope

1.0.0 ·Source§

implCopy forShutdown

1.0.0 ·Source§

implCopy forSocketAddr

1.0.0 ·Source§

implCopy forFpCategory

1.55.0 ·Source§

implCopy forIntErrorKind

Source§

implCopy forBacktraceStyle

Source§

implCopy forSearchStep

1.0.0 ·Source§

implCopy for std::sync::atomic::Ordering

1.12.0 ·Source§

implCopy forRecvTimeoutError

1.0.0 ·Source§

implCopy forTryRecvError

1.0.0 ·Source§

implCopy forbool

1.0.0 ·Source§

implCopy forchar

1.0.0 ·Source§

implCopy forf16

1.0.0 ·Source§

implCopy forf32

1.0.0 ·Source§

implCopy forf64

1.0.0 ·Source§

implCopy forf128

1.0.0 ·Source§

implCopy fori8

1.0.0 ·Source§

implCopy fori16

1.0.0 ·Source§

implCopy fori32

1.0.0 ·Source§

implCopy fori64

1.0.0 ·Source§

implCopy fori128

1.0.0 ·Source§

implCopy forisize

Source§

implCopy for!

1.0.0 ·Source§

implCopy foru8

1.0.0 ·Source§

implCopy foru16

1.0.0 ·Source§

implCopy foru32

1.0.0 ·Source§

implCopy foru64

1.0.0 ·Source§

implCopy foru128

1.0.0 ·Source§

implCopy forusize

1.27.0 ·Source§

implCopy forCpuidResult

1.27.0 ·Source§

implCopy for__m128

1.89.0 ·Source§

implCopy for__m128bh

1.27.0 ·Source§

implCopy for__m128d

Source§

implCopy for__m128h

1.27.0 ·Source§

implCopy for__m128i

1.27.0 ·Source§

implCopy for__m256

1.89.0 ·Source§

implCopy for__m256bh

1.27.0 ·Source§

implCopy for__m256d

Source§

implCopy for__m256h

1.27.0 ·Source§

implCopy for__m256i

1.72.0 ·Source§

implCopy for__m512

1.89.0 ·Source§

implCopy for__m512bh

1.72.0 ·Source§

implCopy for__m512d

Source§

implCopy for__m512h

1.72.0 ·Source§

implCopy for__m512i

Source§

implCopy forbf16

Source§

implCopy forAllocError

Source§

implCopy forGlobal

1.28.0 ·Source§

implCopy forLayout

1.28.0 ·Source§

implCopy forSystem

1.0.0 ·Source§

implCopy forTypeId

1.34.0 ·Source§

implCopy forTryFromSliceError

1.34.0 ·Source§

implCopy forCharTryFromError

1.59.0 ·Source§

implCopy forTryFromCharError

1.0.0 ·Source§

implCopy forError

Source§

implCopy forFormattingOptions

1.75.0 ·Source§

implCopy forFileTimes

1.1.0 ·Source§

implCopy forFileType

1.0.0 ·Source§

implCopy forEmpty

1.0.0 ·Source§

implCopy forSink

Source§

implCopy forAssume

1.0.0 ·Source§

implCopy forIpv4Addr

1.0.0 ·Source§

implCopy forIpv6Addr

1.0.0 ·Source§

implCopy forSocketAddrV4

1.0.0 ·Source§

implCopy forSocketAddrV6

1.34.0 ·Source§

implCopy forTryFromIntError

1.0.0 ·Source§

implCopy forRangeFull

Source§

implCopy forUCred

Available onUnix only.
1.61.0 ·Source§

implCopy forExitCode

1.0.0 ·Source§

implCopy forExitStatus

Source§

implCopy forExitStatusError

Source§

implCopy for std::ptr::Alignment

Source§

implCopy forDefaultRandomSource

1.0.0 ·Source§

implCopy forUtf8Error

1.0.0 ·Source§

implCopy forRecvError

1.5.0 ·Source§

implCopy forWaitTimeoutResult

1.36.0 ·Source§

implCopy forRawWakerVTable

1.26.0 ·Source§

implCopy forAccessError

1.19.0 ·Source§

implCopy forThreadId

1.3.0 ·Source§

implCopy forDuration

1.8.0 ·Source§

implCopy forInstant

1.8.0 ·Source§

implCopy forSystemTime

1.33.0 ·Source§

implCopy forPhantomPinned

1.0.0 ·Source§

impl<'a>Copy forComponent<'a>

1.0.0 ·Source§

impl<'a>Copy forPrefix<'a>

Source§

impl<'a>Copy forUtf8Pattern<'a>

1.0.0 ·Source§

impl<'a>Copy forArguments<'a>

1.36.0 ·Source§

impl<'a>Copy forIoSlice<'a>

1.10.0 ·Source§

impl<'a>Copy forLocation<'a>

1.28.0 ·Source§

impl<'a>Copy forAncestors<'a>

1.0.0 ·Source§

impl<'a>Copy forPrefixComponent<'a>

Source§

impl<'a>Copy forPhantomContravariantLifetime<'a>

Source§

impl<'a>Copy forPhantomCovariantLifetime<'a>

Source§

impl<'a>Copy forPhantomInvariantLifetime<'a>

Source§

impl<'a, T, const N:usize>Copy forArrayWindows<'a, T, N>
where T:Copy + 'a,

1.63.0 ·Source§

impl<'fd>Copy forBorrowedFd<'fd>

Available onUnix or HermitCore ortarget_os=trusty or WASI ortarget_os=motor only.
1.63.0 ·Source§

impl<'handle>Copy forBorrowedHandle<'handle>

Available onWindows only.
1.63.0 ·Source§

impl<'socket>Copy forBorrowedSocket<'socket>

Available onWindows only.
1.55.0 ·Source§

impl<B, C>Copy forControlFlow<B, C>
where B:Copy, C:Copy,

Source§

impl<Dyn>Copy forDynMetadata<Dyn>
where Dyn: ?Sized,

1.28.0 ·Source§

impl<F>Copy forRepeatWith<F>
where F:Copy,

1.0.0 ·Source§

impl<Idx>Copy forRangeTo<Idx>
where Idx:Copy,

1.26.0 ·Source§

impl<Idx>Copy for std::ops::RangeToInclusive<Idx>
where Idx:Copy,

Source§

impl<Idx>Copy forRange<Idx>
where Idx:Copy,

Source§

impl<Idx>Copy forRangeFrom<Idx>
where Idx:Copy,

Source§

impl<Idx>Copy forRangeInclusive<Idx>
where Idx:Copy,

Source§

impl<Idx>Copy for std::range::RangeToInclusive<Idx>
where Idx:Copy,

1.33.0 ·Source§

impl<Ptr>Copy forPin<Ptr>
where Ptr:Copy,

1.17.0 ·Source§

impl<T>Copy forBound<T>
where T:Copy,

1.0.0 ·Source§

impl<T>Copy forOption<T>
where T:Copy,

1.36.0 ·Source§

impl<T>Copy forPoll<T>
where T:Copy,

1.0.0 ·Source§

impl<T>Copy for*const T
where T: ?Sized,

1.0.0 ·Source§

impl<T>Copy for*mut T
where T: ?Sized,

1.0.0 ·Source§

impl<T>Copy for&T
where T: ?Sized,

Shared references can be copied, but mutable referencescannot!

1.19.0 ·Source§

impl<T>Copy forReverse<T>
where T:Copy,

1.21.0 ·Source§

impl<T>Copy forDiscriminant<T>

1.20.0 ·Source§

impl<T>Copy forManuallyDrop<T>
where T:Copy + ?Sized,

1.28.0 ·Source§

impl<T>Copy forNonZero<T>

1.74.0 ·Source§

impl<T>Copy forSaturating<T>
where T:Copy,

1.0.0 ·Source§

impl<T>Copy forWrapping<T>
where T:Copy,

1.25.0 ·Source§

impl<T>Copy forNonNull<T>
where T: ?Sized,

Source§

impl<T>Copy forExclusive<T>
where T:Sync +Copy,

Source§

impl<T>Copy forPhantomContravariant<T>
where T: ?Sized,

Source§

impl<T>Copy forPhantomCovariant<T>
where T: ?Sized,

1.0.0 ·Source§

impl<T>Copy forPhantomData<T>
where T: ?Sized,

Source§

impl<T>Copy forPhantomInvariant<T>
where T: ?Sized,

1.36.0 ·Source§

impl<T>Copy forMaybeUninit<T>
where T:Copy,

1.0.0 ·Source§

impl<T, E>Copy forResult<T, E>
where T:Copy, E:Copy,

1.58.0 ·Source§

impl<T, const N:usize>Copy for[T; N]
where T:Copy,

Source§

impl<T, const N:usize>Copy forMask<T, N>

Source§

impl<T, const N:usize>Copy forSimd<T, N>

Source§

impl<T:Copy>Copy forSendTimeoutError<T>

1.0.0 ·Source§

impl<T:Copy>Copy forTrySendError<T>

1.0.0 ·Source§

impl<T:Copy>Copy forSendError<T>

Source§

impl<Y, R>Copy forCoroutineState<Y, R>
where Y:Copy, R:Copy,


[8]ページ先頭

©2009-2026 Movatter.jp