Avatar ·

How to implement a trait for a type from an external crate if I can't modify either the trait or the type?

Hi everyone! I'm new to Rust and I've run into a problem that I haven't been able to solve for several hours.\n\nI have an external crate `external_crate` that defines a trait `Transformer` and a struct `Data`. I can't modify the code of this crate. It looks something like this:\n\n```rust\n// external_crate\npub trait Transformer {\n fn transform(&self) -> String;\n}\n\npub struct Data {\n pub value: i32,\n}\n```\n\nI want to implement `Transformer` for `Data` to use it in my project. I write:\n\n```rust\nuse external_crate::{Data, Transformer};\n\nimpl Transformer for Data {\n fn transform(&self) -> String {\n format!("Value: {}", self.value)\n }\n}\n```\n\nBut the compiler gives an error:\n\n```\nerror[E0117]: only traits defined in the current crate can be implemented