Skip to content

Instantly share code, notes, and snippets.

@ohmree
Created April 24, 2020 16:47
Show Gist options
  • Save ohmree/a653e6c01229dce92c977a7f727b271d to your computer and use it in GitHub Desktop.
Save ohmree/a653e6c01229dce92c977a7f727b271d to your computer and use it in GitHub Desktop.
#[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