rust-lang / rust-lang/rfcs

Rust Structure and Implementation "Embedding" Brainstorm

Open
#2,431 23 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I've been talking about code reuse in Rust with my brother ( @emmetoneillpdx ) and one of the ideas we considered was a form of "static inheritance" which basically amounts to a syntax for automatically pulling either data or functions (or both) from existing structs and trait implementations. The proposed syntax is roughly based on Rusts' existing "Struct Update Syntax". Here's a simple pseudocode example:

trait Consume {
    fn eat(&self);
    fn drink();
}

struct Animal {
    favorite_food : &'static str,
}

impl Consume for Animal {
    fn eat(&self) {
        println!("I'm eating some {}!", self.favorite_food);
    }

    fn drink() {
        println!("I'm drinking now!");
    }
}

struct Dog {
    name : &'static str,
    ..Animal, // Statically "inherit" any field that is not already defined in Dog from Animal
}

impl Consume for Dog {
    fn drink() { 
        println!("I'm drinking out of a dog bowl!");
    }

    ..Animal // Likewise, statically "inherit" any undefined Consume function implementation for Dog from Animal
                 // Since drink() has already been implemented for Dog, Animal's implementation won't be used.
}
//If Dog tries to inherit an implementation referring to a variable outside of its scope, compile error.
//Fundamentally the same as if you tried to copy + paste the eat(&self) implementation from Animal to Dog. (but without copy and pasting code!)

Allowing for code reuse this way should not fundamentally change the way Rust behaves at runtime but still allows for behavior that is somewhat similar to inheritance without error-prone code duplication practices. Essentially, we're just asking the compiler to copy/paste any undefined fields or functions from any known (at compile time) struct or implementation.

We don't have anything more concrete than that right now, but this is an idea that came up over the course of a conversation and we both wanted to share it and see what people thought. I'd love to hear feedback and form a discussion around this idea or others like it. Also, if you know of any relevant RFCs where it might be appropriate to share this (or is already working on a similar idea), please share.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are identified. Start by reviewing the proposed syntax, the linked Struct Update Syntax reference, and the discussion for feedback or related RFCs. Done would require a concrete, scoped proposal with agreed semantics; this issue currently defines no implementation task.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.