Created
April 24, 2020 16:47
-
-
Save ohmree/a653e6c01229dce92c977a7f727b271d to your computer and use it in GitHub Desktop.
This file contains 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
#[derive(Clone, Copy, Debug)] | |
enum Foo { | |
One, | |
Two, | |
} | |
impl Foo { | |
pub fn get_it(&self) -> &str { | |
match self { | |
Foo::One => "Doodoo", | |
Foo::Two => "Kaka", | |
} | |
} | |
} | |
trait Bar { | |
// This is an implementation detail. | |
fn get_foo(&self) -> Foo; | |
// This is user-facing API that uses the internal `foo` member. | |
fn do_something(&self) { | |
println!("{}", self.get_foo().get_it()) | |
} | |
} | |
struct Quux { | |
foo: Foo, | |
} | |
impl Bar for Quux { | |
fn get_foo(&self) -> Foo { self.foo } | |
} | |
fn main() { | |
let quux = Quux { foo: Foo::One }; | |
// So far so good. | |
quux.do_something(); | |
// WTF? We're not meant to have access to the internal foo variable. | |
println!("{:?}", quux.get_foo()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment