Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Created February 16, 2013 15:37
Show Gist options
  • Select an option

  • Save Kimundi/4967406 to your computer and use it in GitHub Desktop.

Select an option

Save Kimundi/4967406 to your computer and use it in GitHub Desktop.
Rust method variations
mod foobar {
pub trait Foo {
static fn foo(int) -> Self;
fn foo_add(&self,int) -> Self;
}
pub struct Bar {
value: int
}
impl Bar {
static fn bar(v: int) -> Bar { Bar{ value: v } }
fn bar_add(&self, v: int) -> Bar { Bar{ value: (self.value + v) } }
}
impl Foo for Bar {
static fn foo(v: int) -> Bar { Bar{ value: v } }
fn foo_add(&self, v: int) -> Bar { Bar{ value: (self.value + v) } }
}
}
mod foobar_ext {
use super::foobar::Bar;
use super::foobar::Foo;
/*impl Bar {
fn more_bar(&self) -> (Bar, Bar) {
(copy *self, copy *self)
}
}*/
pub trait FooExt {
fn more_foo(&self) -> (Self, Self, Self);
}
impl<T:Foo+Copy> FooExt for T {
fn more_foo(&self) -> (T, T, T) {
(copy *self, copy *self, copy *self)
}
}
}
fn main() {
use foobar::Bar;
use foobar::Foo;
use foobar_ext::FooExt;
let mut x: Bar;
x = Bar::bar(3);
x = x.foo_add(5);
x = x.bar_add(3);
x = Foo::foo(5);
x = x.foo_add(5);
x = x.bar_add(3);
//let y = x.more_bar();
let z = x.more_foo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment