Last active
March 28, 2025 18:34
-
-
Save hbjydev/3db3079a09dd0582926dc704263dd366 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Type you want to build | |
struct MyThing {} | |
trait MyBuilder { | |
type OutputType; | |
fn build(&self) -> OutputType; | |
} | |
struct MyThingBuilder {} | |
impl MyBuilder for MyThingBuilder { | |
type OutputType = MyThing; | |
fn build(&self) -> String { /* ... */ } // Won't compile, returns the wrong type | |
fn build(&self) -> MyThing { /* ... */ } // Will compile, returns the right type | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This pattern allows the trait to be generic without specifying a
MyBuilder<T>
-style thing.