Trait DerefMut

1.6.0 (const: unstable) ยท Source
pub trait DerefMut: Deref {
    // Required method
    fn deref_mut(&mut self) -> &mut Self::Target;
}
Expand description

Used for mutable dereferencing operations, like in *v = 1;.

In addition to being used for explicit dereferencing operations with the (unary) * operator in mutable contexts, DerefMut is also used implicitly by the compiler in many circumstances. This mechanism is called โ€œmutable deref coercionโ€. In immutable contexts, Deref is used.

Warning: Deref coercion is a powerful language feature which has far-reaching implications for every type that implements DerefMut. The compiler will silently insert calls to DerefMut::deref_mut. For this reason, one should be careful about implementing DerefMut and only do so when mutable deref coercion is desirable. See the Deref docs for advice on when this is typically desirable or undesirable.

Types that implement DerefMut or Deref are often called โ€œsmart pointersโ€ and the mechanism of deref coercion has been specifically designed to facilitate the pointer-like behavior that name suggests. Often, the purpose of a โ€œsmart pointerโ€ type is to change the ownership semantics of a contained value (for example, Rc or Cow) or the storage semantics of a contained value (for example, Box).

ยงMutable deref coercion

If T implements DerefMut<Target = U>, and v is a value of type T, then:

  • In mutable contexts, *v (where T is neither a reference nor a raw pointer) is equivalent to *DerefMut::deref_mut(&mut v).
  • Values of type &mut T are coerced to values of type &mut U
  • T implicitly implements all the (mutable) methods of the type U.

For more details, visit the chapter in The Rust Programming Language as well as the reference sections on the dereference operator, method resolution and type coercions.

ยงFallibility

This traitโ€™s method should never unexpectedly fail. Deref coercion means the compiler will often insert calls to DerefMut::deref_mut implicitly. Failure during dereferencing can be extremely confusing when DerefMut is invoked implicitly. In the majority of uses it should be infallible, though it may be acceptable to panic if the type is misused through programmer error, for example.

However, infallibility is not enforced and therefore not guaranteed. As such, unsafe code should not rely on infallibility in general for soundness.

ยงExamples

A struct with a single field which is modifiable by dereferencing the struct.

use std::ops::{Deref, DerefMut};

struct DerefMutExample<T> {
    value: T
}

impl<T> Deref for DerefMutExample<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T> DerefMut for DerefMutExample<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

let mut x = DerefMutExample { value: 'a' };
*x = 'b';
assert_eq!('b', x.value);

Required Methodsยง

1.0.0 ยท Source

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Implementorsยง

ยง

impl DerefMut for OriginalUri

ยง

impl DerefMut for BytesMut

ยง

impl DerefMut for BrowserRequest

Sourceยง

impl DerefMut for ByteStr

Sourceยง

impl DerefMut for ByteString

1.3.0 ยท Sourceยง

impl DerefMut for String

1.44.0 ยท Sourceยง

impl DerefMut for OsString

1.68.0 ยท Sourceยง

impl DerefMut for PathBuf

Sourceยง

impl DerefMut for Error

Sourceยง

impl DerefMut for Report

Sourceยง

impl DerefMut for Buf

Sourceยง

impl DerefMut for Asn1BitString

Sourceยง

impl DerefMut for Asn1Enumerated

Sourceยง

impl DerefMut for Asn1GeneralizedTime

Sourceยง

impl DerefMut for Asn1Integer

Sourceยง

impl DerefMut for Asn1Object

Sourceยง

impl DerefMut for Asn1OctetString

Sourceยง

impl DerefMut for Asn1String

Sourceยง

impl DerefMut for Asn1Time

Sourceยง

impl DerefMut for BigNum

Sourceยง

impl DerefMut for BigNumContext

Sourceยง

impl DerefMut for Cipher

Sourceยง

impl DerefMut for CipherCtx

Sourceยง

impl DerefMut for CmsContentInfo

Sourceยง

impl DerefMut for Conf

Sourceยง

impl DerefMut for DsaSig

Sourceยง

impl DerefMut for EcGroup

Sourceยง

impl DerefMut for EcPoint

Sourceยง

impl DerefMut for EcdsaSig

Sourceยง

impl DerefMut for DigestBytes

Sourceยง

impl DerefMut for LibCtx

Sourceยง

impl DerefMut for Md

Sourceยง

impl DerefMut for MdCtx

Sourceยง

impl DerefMut for OcspBasicResponse

Sourceยง

impl DerefMut for OcspCertId

Sourceยง

impl DerefMut for OcspOneReq

Sourceยง

impl DerefMut for OcspRequest

Sourceยง

impl DerefMut for OcspResponse

Sourceยง

impl DerefMut for Pkcs7

Sourceยง

impl DerefMut for Pkcs7Signed

Sourceยง

impl DerefMut for Pkcs7SignerInfo

Sourceยง

impl DerefMut for Pkcs12

Sourceยง

impl DerefMut for Provider

Sourceยง

impl DerefMut for SrtpProtectionProfile

Sourceยง

impl DerefMut for ConnectConfiguration

Sourceยง

impl DerefMut for SslAcceptorBuilder

Sourceยง

impl DerefMut for SslConnectorBuilder

Sourceยง

impl DerefMut for Ssl

Sourceยง

impl DerefMut for SslCipher

Sourceยง

impl DerefMut for SslContext

Sourceยง

impl DerefMut for SslSession

Sourceยง

impl DerefMut for OpensslString

Sourceยง

impl DerefMut for X509Store

Sourceยง

impl DerefMut for X509StoreBuilder

Sourceยง

impl DerefMut for AccessDescription

Sourceยง

impl DerefMut for DistPoint

Sourceยง

impl DerefMut for DistPointName

Sourceยง

impl DerefMut for GeneralName

Sourceยง

impl DerefMut for X509

Sourceยง

impl DerefMut for X509Algorithm

Sourceยง

impl DerefMut for X509Crl

Sourceยง

impl DerefMut for X509Extension

Sourceยง

impl DerefMut for X509Name

Sourceยง

impl DerefMut for X509NameEntry

Sourceยง

impl DerefMut for X509Object

Sourceยง

impl DerefMut for X509Req

Sourceยง

impl DerefMut for X509Revoked

Sourceยง

impl DerefMut for X509StoreContext

Sourceยง

impl DerefMut for X509VerifyParam

Sourceยง

impl DerefMut for And

Sourceยง

impl DerefMut for At

Sourceยง

impl DerefMut for Caret

Sourceยง

impl DerefMut for Colon

Sourceยง

impl DerefMut for Comma

Sourceยง

impl DerefMut for Dollar

Sourceยง

impl DerefMut for Dot

Sourceยง

impl DerefMut for Eq

Sourceยง

impl DerefMut for Gt

Sourceยง

impl DerefMut for Lt

Sourceยง

impl DerefMut for Minus

Sourceยง

impl DerefMut for Not

Sourceยง

impl DerefMut for Or

Sourceยง

impl DerefMut for Percent

Sourceยง

impl DerefMut for Plus

Sourceยง

impl DerefMut for Pound

Sourceยง

impl DerefMut for Question

Sourceยง

impl DerefMut for Semi

Sourceยง

impl DerefMut for Slash

Sourceยง

impl DerefMut for Star

Sourceยง

impl DerefMut for Tilde

Sourceยง

impl DerefMut for Underscore

ยง

impl DerefMut for AlignedVec

ยง

impl DerefMut for BorrowedPayload<'_>

ยง

impl DerefMut for BoxTokenStream<'_>

ยง

impl DerefMut for ClientConnection

ยง

impl DerefMut for ClientConnection

ยง

impl DerefMut for Connection

ยง

impl DerefMut for Connection

ยง

impl DerefMut for MmapMut

ยง

impl DerefMut for ServerConnection

ยง

impl DerefMut for ServerConnection

ยง

impl DerefMut for SqliteOwnedBuf

ยง

impl DerefMut for UnbufferedClientConnection

ยง

impl DerefMut for UnbufferedServerConnection

ยง

impl DerefMut for Utf8PathBuf

Requires Rust 1.68 or newer.

1.36.0 ยท Sourceยง

impl<'a> DerefMut for IoSliceMut<'a>

ยง

impl<'a> DerefMut for MaybeUninitSlice<'a>

Sourceยง

impl<'a, 'f> DerefMut for VaList<'a, 'f>
where 'f: 'a,

ยง

impl<'a, K, V> DerefMut for RefMut<'a, K, V>
where K: Eq + Hash,

ยง

impl<'a, K, V> DerefMut for RefMutMulti<'a, K, V>
where K: Eq + Hash,

ยง

impl<'a, K, V, S> DerefMut for RefMut<'a, K, V, S>
where K: Eq + Hash, S: BuildHasher,

ยง

impl<'a, K, V, S> DerefMut for RefMutMulti<'a, K, V, S>
where K: Eq + Hash, S: BuildHasher,

ยง

impl<'a, K, V, T> DerefMut for MappedRefMut<'a, K, V, T>
where K: Eq + Hash,

ยง

impl<'a, K, V, T, S> DerefMut for MappedRefMut<'a, K, V, T, S>
where K: Eq + Hash, S: BuildHasher,

ยง

impl<'a, R, T> DerefMut for MappedMutexGuard<'a, R, T>
where R: RawMutex + 'a, T: 'a + ?Sized,

ยง

impl<'a, R, T> DerefMut for MappedRwLockWriteGuard<'a, R, T>
where R: RawRwLock + 'a, T: 'a + ?Sized,

ยง

impl<'a, R, T> DerefMut for MutexGuard<'a, R, T>
where R: RawMutex + 'a, T: 'a + ?Sized,

ยง

impl<'a, R, T> DerefMut for RwLockWriteGuard<'a, R, T>
where R: RawRwLock + 'a, T: 'a + ?Sized,

Sourceยง

impl<'a, T> DerefMut for alloc::vec::peek_mut::PeekMut<'a, T>

ยง

impl<'a, T> DerefMut for Locked<'a, T>

ยง

impl<'a, T> DerefMut for MappedMutexGuard<'a, T>
where T: ?Sized,

ยง

impl<'a, T> DerefMut for MutexGuard<'a, T>
where T: ?Sized,

ยง

impl<'a, T> DerefMut for SpinMutexGuard<'a, T>
where T: ?Sized,

Sourceยง

impl<'a, T, C> DerefMut for sharded_slab::pool::RefMut<'a, T, C>
where T: Clear + Default, C: Config,

ยง

impl<'a, T, F> DerefMut for PoolGuard<'a, T, F>
where T: Send, F: Fn() -> T,

Sourceยง

impl<'a, T, U> DerefMut for FromColorMutGuard<'a, T, U>
where T: FromColorMut<U> + ?Sized, U: FromColorMut<T> + ?Sized,

Sourceยง

impl<'a, T, U> DerefMut for FromColorUnclampedMutGuard<'a, T, U>

ยง

impl<'c, DB> DerefMut for MaybePoolConnection<'c, DB>
where DB: Database,

ยง

impl<'c, DB> DerefMut for Transaction<'c, DB>
where DB: Database,

ยง

impl<'i> DerefMut for DeArray<'i>

ยง

impl<'rwlock, T, R> DerefMut for RwLockWriteGuard<'rwlock, T, R>
where T: ?Sized,

ยง

impl<A> DerefMut for SmallVec<A>
where A: Array,

ยง

impl<B, T> DerefMut for Ref<B, T>
where B: ByteSliceMut, T: FromBytes + IntoBytes + KnownLayout + Immutable + ?Sized,

Sourceยง

impl<C> DerefMut for PreAlpha<C>
where C: Premultiply,

Sourceยง

impl<C, T> DerefMut for Alpha<C, T>

ยง

impl<DB> DerefMut for PoolConnection<DB>
where DB: Database,

ยง

impl<Data> DerefMut for ConnectionCommon<Data>

ยง

impl<E, T> DerefMut for Targeted<E, T>

ยง

impl<F> DerefMut for DebugFn<F>
where F: ?Sized,

ยง

impl<F, A> DerefMut for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

ยง

impl<Inner, Prev, K, T, Guard> DerefMut for KeyedSubfieldWriteGuard<Inner, Prev, K, T, Guard>
where Guard: DerefMut, KeyedSubfield<Inner, Prev, K, T>: Clone, &'a T: for<'a> IntoIterator, Inner: StoreField<Value = Prev>, Prev: 'static, K: Debug + Send + Sync + PartialEq + Eq + Hash + 'static,

ยง

impl<Inner, U> DerefMut for MappedMut<Inner, U>
where Inner: DerefMut,

ยง

impl<Inner, U> DerefMut for MappedMutArc<Inner, U>
where Inner: DerefMut,

ยง

impl<K, V, S> DerefMut for AHashMap<K, V, S>

Sourceยง

impl<L, R> DerefMut for Either<L, R>
where L: DerefMut, R: DerefMut<Target = <L as Deref>::Target>,

ยง

impl<MutexType, T> DerefMut for GenericMutexGuard<'_, MutexType, T>
where MutexType: RawMutex,

1.33.0 ยท Sourceยง

impl<Ptr> DerefMut for Pin<Ptr>
where Ptr: DerefMut, <Ptr as Deref>::Target: Unpin,

ยง

impl<S> DerefMut for State<S>

ยง

impl<S> DerefMut for Ascii<S>

ยง

impl<S> DerefMut for BlockingStream<S>
where S: Stream + Unpin,

ยง

impl<S> DerefMut for UniCase<S>

ยง

impl<S, G> DerefMut for WriteGuard<S, G>
where S: Notify, G: DerefMut,

1.0.0 ยท Sourceยง

impl<T> !DerefMut for &T
where T: ?Sized,

1.0.0 (const: unstable) ยท Sourceยง

impl<T> DerefMut for &mut T
where T: ?Sized,

ยง

impl<T> DerefMut for UntrackedWriteGuard<T>

ยง

impl<T> DerefMut for DoubleDeref<T>
where T: DerefMut, <T as Deref>::Target: DerefMut,

ยง

impl<T> DerefMut for ConnectInfo<T>

ยง

impl<T> DerefMut for Path<T>

ยง

impl<T> DerefMut for Query<T>

ยง

impl<T> DerefMut for Extension<T>

ยง

impl<T> DerefMut for Form<T>

ยง

impl<T> DerefMut for flams_router_vscode::server_fn::axum_export::Json<T>

1.0.0 (const: unstable) ยท Sourceยง

impl<T> DerefMut for flams_router_vscode::server_fn::inventory::core::cell::RefMut<'_, T>
where T: ?Sized,

1.20.0 ยท Sourceยง

impl<T> DerefMut for ManuallyDrop<T>
where T: ?Sized,

1.9.0 ยท Sourceยง

impl<T> DerefMut for AssertUnwindSafe<T>

Sourceยง

impl<T> DerefMut for ThinBox<T>
where T: ?Sized,

Sourceยง

impl<T> DerefMut for std::sync::nonpoison::mutex::MappedMutexGuard<'_, T>
where T: ?Sized,

Sourceยง

impl<T> DerefMut for std::sync::nonpoison::mutex::MutexGuard<'_, T>
where T: ?Sized,

Sourceยง

impl<T> DerefMut for std::sync::poison::mutex::MappedMutexGuard<'_, T>
where T: ?Sized,

1.0.0 ยท Sourceยง

impl<T> DerefMut for std::sync::poison::mutex::MutexGuard<'_, T>
where T: ?Sized,

Sourceยง

impl<T> DerefMut for std::sync::poison::rwlock::MappedRwLockWriteGuard<'_, T>
where T: ?Sized,

1.0.0 ยท Sourceยง

impl<T> DerefMut for std::sync::poison::rwlock::RwLockWriteGuard<'_, T>
where T: ?Sized,

Sourceยง

impl<T> DerefMut for Dh<T>

Sourceยง

impl<T> DerefMut for Dsa<T>

Sourceยง

impl<T> DerefMut for EcKey<T>

Sourceยง

impl<T> DerefMut for PKey<T>

Sourceยง

impl<T> DerefMut for PkeyCtx<T>

Sourceยง

impl<T> DerefMut for Rsa<T>

Sourceยง

impl<T> DerefMut for Stack<T>
where T: Stackable,

Sourceยง

impl<T> DerefMut for X509Lookup<T>

Sourceยง

impl<T> DerefMut for X509LookupMethod<T>

Sourceยง

impl<T> DerefMut for Clamped<T>

ยง

impl<T> DerefMut for ArcMutexGuardian<T>
where T: ?Sized,

ยง

impl<T> DerefMut for ArcRwLockWriteGuardian<T>
where T: ?Sized,

ยง

impl<T> DerefMut for CachePadded<T>

ยง

impl<T> DerefMut for CommaSeparatedList<T>

ยง

impl<T> DerefMut for ConnectionCommon<T>

ยง

impl<T> DerefMut for Json<T>

ยง

impl<T> DerefMut for MutexGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for MutexGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for MutexGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for MutexGuardArc<T>
where T: ?Sized,

ยง

impl<T> DerefMut for OptionalProp<T>

ยง

impl<T> DerefMut for Owned<T>
where T: Pointable + ?Sized,

ยง

impl<T> DerefMut for OwnedMutexGuard<T>
where T: ?Sized,

ยง

impl<T> DerefMut for OwnedMutexGuard<T>
where T: ?Sized,

ยง

impl<T> DerefMut for OwnedRwLockWriteGuard<T>
where T: ?Sized,

ยง

impl<T> DerefMut for RcMutexGuardian<T>
where T: ?Sized,

ยง

impl<T> DerefMut for RcRwLockWriteGuardian<T>
where T: ?Sized,

ยง

impl<T> DerefMut for RwLockMappedWriteGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for RwLockWriteGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for RwLockWriteGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for RwLockWriteGuardArc<T>
where T: ?Sized,

ยง

impl<T> DerefMut for ScratchVec<T>

ยง

impl<T> DerefMut for SendOption<T>

ยง

impl<T> DerefMut for SendWrapper<T>

ยง

impl<T> DerefMut for ShardedLockWriteGuard<'_, T>
where T: ?Sized,

ยง

impl<T> DerefMut for Text<T>

ยง

impl<T> DerefMut for Unalign<T>
where T: Unaligned,

ยง

impl<T> DerefMut for UniqueArc<T>
where T: ?Sized,

1.0.0 ยท Sourceยง

impl<T, A> DerefMut for alloc::boxed::Box<T, A>
where A: Allocator, T: ?Sized,

1.12.0 ยท Sourceยง

impl<T, A> DerefMut for alloc::collections::binary_heap::PeekMut<'_, T, A>
where T: Ord, A: Allocator,

Sourceยง

impl<T, A> DerefMut for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

Sourceยง

impl<T, A> DerefMut for alloc::sync::UniqueArc<T, A>
where A: Allocator, T: ?Sized,

1.0.0 ยท Sourceยง

impl<T, A> DerefMut for alloc::vec::Vec<T, A>
where A: Allocator,

ยง

impl<T, A> DerefMut for Box<T, A>
where A: Allocator, T: ?Sized,

ยง

impl<T, A> DerefMut for Vec<T, A>
where A: Allocator,

Sourceยง

impl<T, C> DerefMut for OwnedRefMut<T, C>
where T: Clear + Default, C: Config,

ยง

impl<T, E> DerefMut for BoxedStream<T, E>

1.89.0 ยท Sourceยง

impl<T, F> DerefMut for LazyCell<T, F>
where F: FnOnce() -> T,

Sourceยง

impl<T, F> DerefMut for DropGuard<T, F>
where F: FnOnce(T),

1.89.0 ยท Sourceยง

impl<T, F> DerefMut for LazyLock<T, F>
where F: FnOnce() -> T,

ยง

impl<T, F> DerefMut for Lazy<T, F>
where F: FnOnce() -> T,

ยง

impl<T, F> DerefMut for Lazy<T, F>
where F: FnOnce() -> T,

Sourceยง

impl<T, F, S> DerefMut for ScopeGuard<T, F, S>
where F: FnOnce(T), S: Strategy,

ยง

impl<T, N> DerefMut for GenericArray<T, N>
where N: ArrayLength<T>,

ยง

impl<T, S> DerefMut for AHashSet<T, S>

ยง

impl<T, Ser> DerefMut for SharedValue<T, Ser>

ยง

impl<T, U> DerefMut for MappedMutexGuard<'_, T, U>
where T: ?Sized, U: ?Sized,

ยง

impl<T, U> DerefMut for OwnedMappedMutexGuard<T, U>
where T: ?Sized, U: ?Sized,

ยง

impl<T, U> DerefMut for OwnedRwLockMappedWriteGuard<T, U>
where T: ?Sized, U: ?Sized,

ยง

impl<T, const N: usize> DerefMut for SmallVec<T, N>

ยง

impl<Z> DerefMut for Zeroizing<Z>
where Z: Zeroize,

ยง

impl<const N: usize> DerefMut for AlignedBytes<N>