WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Rust · Intermediate · question 37 of 100

What is the purpose of the Deref and DerefMut traits, and when would you implement them?

📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

In Rust, the ‘Deref‘ and ‘DerefMut‘ traits provide a convenient way to work with references to structs or enums that contain data.

The ‘Deref‘ trait lets you customize the behavior of the dereference operator (‘*‘) and allows a type to be treated like a reference to an inner value. For example, suppose we have a ‘Box<Vec<i32>>‘. If we have a function that accepts a ‘&Vec<i32>‘, we could use the ‘Deref‘ implementation on the ‘Box‘ type to pass the inner ‘Vec‘ to the function instead of the ‘Box‘ itself.

Here is an example implementation of the ‘Deref‘ trait on a custom ‘Wrapper‘ type:

    struct Wrapper<T>(T);
    
    impl<T> Deref for Wrapper<T> {
        type Target = T;
        
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

This implementation tells Rust that when we apply the dereference operator (‘*‘) to a ‘Wrapper<T>‘ instance, we want to return a reference to the inner ‘T‘ value.

The ‘DerefMut‘ trait provides the same functionality as ‘Deref‘, but it’s used for mutable references. This lets you modify the inner value through the reference.

Here is an example implementation of the ‘DerefMut‘ trait on a custom ‘Counter‘ type:

    struct Counter {
        count: usize,
    }
    
    impl Deref for Counter {
        type Target = usize;
        
        fn deref(&self) -> &Self::Target {
            &self.count
        }
    }
    
    impl DerefMut for Counter {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.count
        }
    }

In this example, the ‘Deref‘ implementation provides an immutable reference to the ‘count‘ field of the ‘Counter‘ struct. The ‘DerefMut‘ implementation provides a mutable reference to the same field so that we can modify it through the reference.

You would implement these traits on your own types when you want to customize how that type is dereferenced. This can be useful if you have a type that contains an inner value that is frequently accessed and you want to provide convenience methods for passing references to that inner value.

For example, if you have a ‘MyVec‘ type that wraps around a standard Rust ‘Vec‘, you could implement ‘Deref‘ to provide a reference to the inner ‘Vec‘ instance, allowing users to access the methods of ‘Vec‘ directly from ‘MyVec‘. Similarly, you could implement ‘DerefMut‘ to allow modifying the inner ‘Vec‘ instance through the ‘MyVec‘ instance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Rust interview — then scores it.
📞 Practice Rust — free 15 min
📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

All 100 Rust questions · All topics