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

Rust · Expert · question 62 of 100

How does Rust’s coherence rule affect the implementation of traits for external types?

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

Rust’s coherence rule ensures that there is only one implementation of a trait for a given combination of a type and a trait. When implementing a trait for an external type, the coherence rule can have a significant impact on the implementation.

Let’s say we want to implement the ‘Display‘ trait for an external type ‘MyType‘. If ‘MyType‘ is defined in a separate crate, we cannot modify its definition to add a ‘Display‘ implementation in the traditional way. Instead, we have to use the ‘impl‘ keyword to implement the trait outside of the crate. For example:

    extern crate my_crate;
    
    impl std::fmt::Display for my_crate::MyType {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            // implementation here
        }
    }

However, if another crate also uses ‘MyType‘ and tries to implement ‘Display‘ for it, the coherence rule will prevent this. Rust will flag the conflicting implementation as a compile error. This can be an issue in cases where multiple crates are trying to implement the same trait for the same type, but there can only be one valid implementation according to the coherence rules.

To work around this, Rust provides the ability to "orphan" a trait implementation. This is done by defining a new trait, which extends the existing trait, and then implementing the new trait for the external type. Since the new trait is not the same as the original trait, it does not conflict with any other implementations. For example:

    extern crate my_crate;
    
    trait MyDisplay: std::fmt::Display {}
    
    impl MyDisplay for my_crate::MyType {}
    
    // Now we can use MyDisplay instead of Display
    fn some_function<T: MyDisplay>(item: T) {
        // ...
    }

By using this approach, we can safely implement traits for external types without worrying about conflicting implementations from other crates. However, it does require creating a separate trait and modifying any code that uses the original trait to use the new trait.

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