Skip to content

Fixes bounds/bounds.rs #348

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 3 commits into from
Jan 8, 2015
Merged
Show file tree
Hide file tree
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
37 changes: 16 additions & 21 deletions examples/bounds/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
#[deriving(Show)]
#[deriving(Show, Copy)]
struct Vec2<T> {
x: T,
y: T,
}

impl<
// Bound: `T` must implement the `Add` trait
T: Add<T, T>
> Add<Vec2<T>, Vec2<T>>
for Vec2<T> {
fn add(&self, rhs: &Vec2<T>) -> Vec2<T> {
// Apply bound to `T` at first instance of `T`. `T`
// must implement the `Add` trait.
impl<T: Add<T, T>> Add<Vec2<T>, Vec2<T>>
for Vec2<T> {
fn add(self, rhs: Vec2<T>) -> Vec2<T> {
Vec2 {
// `x` and `y` are of type `T`, and implement the `add` method
x: self.x.add(&rhs.x),
x: self.x.add(rhs.x),
// The sugary `+` operator can also be used
y: self.y + rhs.y,
}
}
}

impl<
// Bound: `T` must implement the `Sub` trait
T: Sub<T, T>
> Sub<Vec2<T>, Vec2<T>>
for Vec2<T> {
fn sub(&self, rhs: &Vec2<T>) -> Vec2<T> {
// Bound: `T` must implement the `Sub` trait
impl<T> Sub<Vec2<T>, Vec2<T>> for Vec2<T>
where T: Sub<T, T> {
fn sub(self, rhs: Vec2<T>) -> Vec2<T> {
Vec2 {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}

impl<
// Bound: `T` must implement *both* the `Add` trait and the `Mul` trait
T: Add<T, T> + Mul<T, T>
> Mul<Vec2<T>, T>
for Vec2<T> {
fn mul(&self, rhs: &Vec2<T>) -> T {
// Bound: `T` must implement *both* the `Add` trait and the `Mul` trait
impl<T> Vec2<T>
where T: Add<T, T> + Mul<T, T> {
fn dot(self, rhs: Vec2<T>) -> T {
(self.x * rhs.x) + (self.y * rhs.y)
}
}
Expand All @@ -49,7 +44,7 @@ fn main() {

println!("{} + {} = {}", v1, v2, v1 + v2);
println!("{} - {} = {}", v1, v2, v1 - v2);
println!("{} . {} = {}", v1, v2, v1.dot(&v2));
println!("{} {} = {}", v1, v2, v1.dot(v2));

// Error! `char` doesn't implement the `Add` trait
println!("{}", Vec2 { x: ' ', y: 'b' } + Vec2 { x: 'c', y: 'd' });
Expand Down
5 changes: 5 additions & 0 deletions examples/bounds/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ When working with generics, the type parameters (e.g. `Ty`) may use traits
* The generic can only be specialized for type parameters that conform to the
bounds.

Bounds are typically applied in one of two ways:

* At the first instance of the type
* In a `where` clause which directly precedes the `{` in the `impl`

{bounds.play}
5 changes: 2 additions & 3 deletions examples/ops/input.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
In Rust, many of the operators can be overloaded via traits. This is possible
because operators are just sugar for method calls. For example, `a + b`
desugars to `a.add(&b)`. This `add` method is part of the `Add` trait, hence
desugars to `a.add(b)`. This `add` method is part of the `Add` trait, hence
any implementor of the `Add` trait will be able to use the `+` operator.

{operator.play}

Here is a [list][ops] of
the traits that overload operators.
Here is a [list][ops] of the traits that overload operators.

[ops]: http://doc.rust-lang.org/core/ops/
4 changes: 2 additions & 2 deletions examples/ops/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct BarFoo;
// This block implements the operation: Foo + Bar = FooBar
impl Add<Bar, FooBar> for Foo {
fn add(self, _rhs: Bar) -> FooBar {
println!("> Foo.add(&Bar) was called");
println!("> Foo.add(Bar) was called");

FooBar
}
Expand All @@ -23,7 +23,7 @@ impl Add<Bar, FooBar> for Foo {
// This block implements the operation: Bar + Foo = BarFoo
impl Add<Foo, BarFoo> for Bar {
fn add(self, _rhs: Foo) -> BarFoo {
println!("> Bar.add(&Foo) was called");
println!("> Bar.add(Foo) was called");

BarFoo
}
Expand Down