Skip to content

Default methods example: Show "(in)valid" case #27284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/doc/trpl/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,40 +347,50 @@ easiest just to show an example:

```rust
trait Foo {
fn bar(&self);
fn is_valid(&self) -> bool;

fn baz(&self) { println!("We called baz."); }
fn is_invalid(&self) -> bool { !self.is_valid() }
}
```

Implementors of the `Foo` trait need to implement `bar()`, but they don’t
need to implement `baz()`. They’ll get this default behavior. They can
Implementors of the `Foo` trait need to implement `is_valid()`, but they don’t
need to implement `is_invalid()`. They’ll get this default behavior. They can
override the default if they so choose:

```rust
# trait Foo {
# fn bar(&self);
# fn baz(&self) { println!("We called baz."); }
# fn is_valid(&self) -> bool;
#
# fn is_invalid(&self) -> bool { !self.is_valid() }
# }
struct UseDefault;

impl Foo for UseDefault {
fn bar(&self) { println!("We called bar."); }
fn is_valid(&self) -> bool {
println!("Called UseDefault.is_valid.");
true
}
}

struct OverrideDefault;

impl Foo for OverrideDefault {
fn bar(&self) { println!("We called bar."); }
fn is_valid(&self) -> bool {
println!("Called OverrideDefault.is_valid.");
true
}

fn baz(&self) { println!("Override baz!"); }
fn is_invalid(&self) -> bool {
println!("Called OverrideDefault.is_invalid!");
true // this implementation is a self-contradiction!
}
}

let default = UseDefault;
default.baz(); // prints "We called baz."
assert!(!default.is_invalid()); // prints "Called UseDefault.is_valid."

let over = OverrideDefault;
over.baz(); // prints "Override baz!"
assert!(over.is_invalid()); // prints "Called OverrideDefault.is_invalid!"
```

# Inheritance
Expand Down