Skip to content

Default implementations on traits #1

Description

@tarrball

Are there code redundancies that could be cleaned up with default implementations on traits?

https://doc.rust-lang.org/book/ch17-03-oo-design-patterns.html

trait State {
    // default implementation
    fn content<'a>(&self, post: &'a Post) -> &'a str {
        ""
    }
}

struct Published {}

impl State for Published {
    fn content<'a>(&self, post: &'a Post) -> &'a str {
        &post.content
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions