Is it possible in Rust to extend an existing struct while keeping all its fields?
Hello! I just started learning Rust after C++ and Java, and I have a problem.\n\nIn C++ I could do this:\n```cpp\nstruct Base {\n int x;\n int y;\n};\n\nstruct Derived : Base {\n int z;\n};\n```\nAnd all fields from Base automatically ended up in Derived. In Java it's the same with class inheritance.\n\nIn Rust I tried to do something similar:\n```rust\nstruct Base {\n x: i32,\n y: i32,\n}\n\nstruct Derived {\n // I want x and y from Base to be here\n z: i32,\n}\n```\n\nBut the compiler complains that I didn't declare fields x and y. I understand that Rust doesn't have inheritance in the classical sense, but I need to reuse fields from one struct in another.\n\nIt seems to me that copy-pasting all fields is a terrible practice. Imagine that Base has 20 fields, and I want to make 5 different
No answers yet.
Log in to leave an answer